ios - How to programmatically display a view -- with an "x" button to close the view? -
i trying programmatically display view containing image , "x" button -- in code labeled *btndismiss -- close view.
here issue: code display image , "x" button. however, clicking "x" button doesn't close view -- both image , "x" button remain visible.
i'm new xcode, please provide verbose responses (if leave out answer, might not able fill in blanks!)
note: if have solution how show imageview "x" button make both imageview , "x" button disappear, welcome.
here code:
#import "xyzviewcontroller.h" @interface xyzviewcontroller () @end @implementation xyzviewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. uiviewcontroller *vc = [[uiviewcontroller alloc] init]; [self presentviewcontroller:vc animated:yes completion:nil]; uiimageview *bgimage = [[uiimageview alloc] initwithframe:cgrectmake(5, 5, 200, 200)]; bgimage.image = [uiimage imagenamed:@"image"]; [self.view addsubview:bgimage]; uibutton *btndismiss = [[uibutton alloc] initwithframe:cgrectmake(10, 10, 100, 100)]; [btndismiss settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal]; [btndismiss settitle:@"x" forstate:uicontrolstatenormal]; [btndismiss addtarget:self action:@selector(dismissvc:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btndismiss]; } -(void)dismissvc:(id)sender { [self dismissviewcontrolleranimated:yes completion:nil]; }
thanks in advance!
your code set dismiss view controller presented modally. views want remove subviews of self.view
.
you can remove uiimageview
, uibutton
keeping outlets them , calling removefromsuperview
in remove method. method, don't need present or dismiss view controller.
-(void)removeviews:(id)sender { [self.xbutton removefromsuperview]; [self.imageview removefromsuperview]; }
edit in response comment
you'll need have properties declared xbutton , imageview
@property uibutton *xbutton; @property uiimageview *imageview;
edit show full code
@interface mjnviewcontroller () @property uibutton *xbutton; @property uiimageview *imageview; @end @implementation mjnviewcontroller - (void)viewdidload { [super viewdidload]; self.imageview = [[uiimageview alloc] initwithframe:cgrectmake(5, 5, 200, 200)]; self.imageview.image = [uiimage imagenamed:@"myimage"]; [self.view addsubview:self.imageview]; self.xbutton = [[uibutton alloc] initwithframe:cgrectmake(10, 10, 100, 100)]; [self.xbutton settitle:@"x" forstate:uicontrolstatenormal]; [self.xbutton settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal]; [self.xbutton addtarget:self action:@selector(removeviews:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:self.xbutton]; } -(void)removeviews:(id)sender { [self.imageview removefromsuperview]; [self.xbutton removefromsuperview]; } @end
Comments
Post a Comment