ios - How to show viewcontroller from the view? -
i searched answers get uiviewcontroller uiview? , couple of other answers not successful.
my issue have button in uiview
lets class1
, when click on button want load view class2
uiviewcontroller
, , don't navigationcontroller
in class1
unable load class2
view.
please me this.
thanks, in advance.
in general uiviews
should not contain logic triggers flow of app. job of uiviewcontrollers
. it's way of making design of code better , more organized.
one way use use delegate
pattern in custom uiviews. here s simple setup:
in mycustomview .h file:
@class mycustomview; @protocol mycustomviewdelegate <nsobject> @optional - (void)myviewdidtaponbutton:(mycustomview)mycustomview; @end @interface mycustomview : uiview @property (weak, nonatomic) id <mycustomviewdelegate> delegate; @end
in mycustomview .m file:
- (ibaction)didtapmybutton:(id)sender { if ([self.delegate respondstoselector:@selector(myviewdidtaponbutton:)]) { [self.delegate myviewdidtaponbutton:self]; } }
then in viewcontroller, presenting view:
interface:
@interface myviewcontroller ()<mycustomviewdelegate> @property (weak, nonatomic) iboutlet uiview *mycustomview;
and implementation:
- (void)viewdidload { [super viewdidload]; self.mycustomview.delegate = self; } - (void)myviewdidtaponbutton:(mycustomview)mycustomview { ... code presenting viewcontroller ... }
note: if dont use parameter mycustomview
sent in method, common pattern , habit send sender
of delegate first parameter.
this used lot apple, e.g. in
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath;
Comments
Post a Comment