2013-04-13 68 views
0

我有两个uiviewcontrollerMainViewControllerSecondaryViewControlle。在MainViewController我做的:事件代表团

[self.view addSubView:SecondaryViewControlle.view]; 

SecondaryViewController是按功能来通过MainViewController执行的按钮。怎么做?

@protocol SecondViewControlleDelegate 

- (void) doSomething 

@end 

您还需要为“delegate”伊娃添加到您的SecondViewControlle .h文件中:

回答

0

选项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 
    } 
} 
+0

。谢谢 –

+0

它的作品的主题使用选项B中得到回报 –

1

你会在你的SecondViewControlle.h文件中定义的协议,像开始。这将是该委托行:

@interface SecondViewControlle : UIViewController 

... 
... 
... 

@property (nonatomic, assign) id delegate; // all you need to do is add this line inside your interface declarations 

... 
... 
... 
@end 

然后,当你创建/从你的MainViewController实例化SecondaryViewControlle,做出一定的MainViewController添加为委托像这样:

SecondaryViewControlle.delegate = self; 
[self.view addSubView:SecondaryViewControlle.view]; 

现在的“delegate “你的SecondaryViewControlle视图控制器指向你的MainViewController。

,并按下按钮时,你可以简单地这样做:

- (IBAction) buttonIsPressed: (id) sender 
{ 
    [delegate doSomething]; 
} 

现在,我要在这里给你一些建议。

1)不要使用类名称作为对象名称。与其命名为“SecondViewControlle”的对象不同,命名为不同的(并以小写字母开头,即Objective-C约定),如“moreDetailVC”。

2)我已经告诉过你如何用委托模式来做到这一点,但这可能不是做你想做的任何事情的最合适的方式。毕竟,MainViewController对象(应将其重命名为mainVC以区分对象和类)不在屏幕上或可见,因此可能有更好的地方来放置功能?

+1

+1浪费你的时间在这,我们尽力如果我一直没有委托 – Eugene