How to remove subviews in Objective-C?

前端 未结 3 1137
小鲜肉
小鲜肉 2021-02-01 16:47

I have added UIButton and UITextView as subviews to my view programmatically.

notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notes         


        
3条回答
  •  广开言路
    2021-02-01 17:49

    I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.

    In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:

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

    Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.

提交回复
热议问题