2010-06-09 34 views
17

Tôi đã thêm UIButton, UITextView làm chế độ xem phụ vào chế độ xem của tôi, theo chương trình.cách xóa các chế độ xem phụ

notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)]; 
notesDescriptionView.backgroundColor=[UIColor redColor]; 
[self.view addSubview:notesDescriptionView]; 

textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)]; 
[self.view addSubview:textView]; 
printf("\n description button \n"); 

button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button 
    addTarget:self action:@selector(cancel:) 
    forControlEvents:UIControlEventTouchDown]; 
[button setTitle:@"OK" forState:UIControlStateNormal]; 
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0); 
[self.view addSubview:button]; 

đây nếu chúng ta bấm nút, tôi cần phải loại bỏ tất cả các quan điểm phụ,

tôi đang cố gắng

[self.view removeFromSuperView] 

nhưng nó không làm việc

bất kỳ sự giúp đỡ.

Trả lời

16

Tôi giả sử bạn đang gọi số [self.view removeFromSuperView] từ phương thức trong cùng một lớp với đoạn mã trên.

Trong trường hợp đó, [self.view removeFromSuperView] sẽ tự xóa khỏi chế độ giám sát của riêng mình, nhưng bản thân là đối tượng mà bạn muốn loại bỏ chế độ xem phụ. Nếu bạn muốn loại bỏ tất cả các subviews của đối tượng, bạn cần phải làm điều này thay vì:

[notesDescriptionView removeFromSuperview]; 
[button.view removeFromSuperview]; 
[textView removeFromSuperview]; 

Có lẽ bạn muốn để lưu trữ những subviews trong một NSArray và vòng lặp qua mảng gọi removeFromSuperview trên mỗi phần tử trong đó mảng.

+0

còn nút, tôi đã tạo bằng cách thêm làm chế độ xem phụ. thực sự tôi đang cố gắng hiển thị chế độ xem với chế độ xem văn bản, khi nhấn nút, sau khi tôi nhấp vào nút khác ở chế độ xem hiện tại, nó phải quay lại chế độ xem trước đó của tôi. – mac

+0

tôi đã thử [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; nhưng không hoạt động. – mac

+0

Cách nào đúng để gọi một phương thức loại bỏ chế độ xem phụ từ superview khỏi lớp khác? Tôi đang thử phương pháp đại biểu nhưng nó không hoạt động. – SanitLee

56

để loại bỏ tất cả các subviews bạn đã thêm vào quan điểm

sử dụng đoạn mã sau

for (UIView *view in [self.view subviews]) 
    { 
    [view removeFromSuperview]; 
    } 
+1

điều này phải được chấp nhận trả lời! – Umitk

7

Tôi đã luôn luôn được ngạc nhiên rằng C API Mục tiêu không có một phương pháp đơn giản để loại bỏ tất cả lượt xem phụ từ UIView. (API Flash làm, và bạn kết thúc cần nó khá một chút.)

Anwyay, đây là phương pháp helper nhỏ mà tôi sử dụng cho rằng:

- (void)removeAllSubviewsFromUIView:(UIView *)parentView 
{ 
    for (id child in [parentView subviews]) 
    { 
    if ([child isMemberOfClass:[UIView class]]) 
    { 
     [child removeFromSuperview]; 
    } 
    } 
} 

EDIT: chỉ tìm thấy một giải pháp thanh lịch hơn ở đây: What is the best way to remove all subviews from you self.view?

Am sử dụng mà hiện nay như sau:

// Make sure the background and foreground views are empty: 
    [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
    [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

tôi thích t mũ tốt hơn ...

Erik

Các vấn đề liên quan