选项A
这是更快,更容易,但缺乏维护,因为没有合同,说明SecondaryViewController需要不厌其烦调用任何东西,self.parentViewController可以是任何的UIViewController。
选项B
委托模式;这是我的偏好,很明显发生了什么,需要什么,还有一个很好的合同,说明如果你想初始化我,给我一个委托。
选项C
如果SecondaryViewController已通知多个对象,这将是快速使用NSNotificationCenter,但与选项A,没有合同,如果你需要通知很多对象,你需要记住监听对这些对象的通知 - 因为这不是问题,我就不细讲,只是这里的信息
选项A
在MainViewController.m,做一些像这样:
SecondaryViewController *viewcontroller = [[SecondaryViewController alloc] initWithNibName:@"SecondaryView" bundle:nil];
[self addChildViewController:viewcontroller];
//set viewcontroller.view frame
[self.view addSubview:viewcontroller.view];
[viewcontroller didMoveToParentViewController:self];
内部MainViewController.h
-(void) performButtonClickAction;
内部MainViewController.m:
-(void) performButtonClickAction {
//Do something constructive
}
然后SecondaryViewController.m内:
-(IBAction) buttonPressed:(id) sender {
[self.parentViewController performButtonClickAction];
}
选项B
内部SecondaryViewController.h
@protocol SecondaryViewControllerDelegate <NSObject>
-(void) eventAFromViewController:(UIViewController *) viewController;
-(void) eventBFromViewController:(UIViewController *) viewController;
@end
@interface SecondaryViewController : UIViewController {
id<SecondaryViewControllerDelegate> delegate;
}
@property (assign, nonatomic) id<SecondaryViewControllerDelegate> delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(id<SecondaryViewControllerDelegate>) theDelegate;
@end
内部SecondaryViewController.m @synthesize代表= _delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(id<SecondaryViewControllerDelegate>) theDelegate
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.delegate = theDelegate;
}
return self;
}
-(IBAction) buttonPressed:(id) sender {
if(self.delegate != nil) {
[_delegate eventAFromViewController:self];
}
else {
//No delegate
}
}
。谢谢 –
它的作品的主题使用选项B中得到回报 –