事件授权

时间:2013-04-13 20:16:37

标签: iphone ios objective-c

我有两个uiviewcontrollerMainViewControllerSecondaryViewControlle。在MainViewController我做:

[self.view addSubView:SecondaryViewControlle.view];

通过按SecondaryViewController执行的功能,MainViewController是一个按钮。怎么办?

2 个答案:

答案 0 :(得分:1)

首先在SecondViewControlle.h文件中定义一个协议,如:

@protocol SecondViewControlleDelegate

- (void) doSomething

@end

您还需要在SecondViewControlle .h文件中添加“delegate”ivar。这将是代表行:

@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];

现在,您的SecondaryViewControlle视图控制器的“delegate”指向您的MainViewController。

按下按钮后,您可以执行以下操作:

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

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

1)不要使用类名作为对象名。不要使用名为“SecondViewControlle”的对象,而是将其命名为不同的东西(并使用小写,即Objective-C约定开始),类似于“moreDetailVC”。

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

答案 1 :(得分: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 = _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
    }
}
相关问题