2013-04-13 67 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