Ios 使用UIView中的按钮从另一个ViewController触发方法

Ios 使用UIView中的按钮从另一个ViewController触发方法,ios,objective-c,iphone,uiview,Ios,Objective C,Iphone,Uiview,我遇到的问题是,我希望通过单击UIView(页脚)中的按钮来调用navigationController中的方法。当我按下按钮时,它应该调用我试图访问的方法,以打开下面代码中的录像机 我被告知可以实现委托方法或使用NSNotification。以下是我所拥有的: 我的页脚(ESPhotoDetailsFooterView.m)有我创建的按钮。我的页脚只包含一个UIView 我试图在页脚中访问的方法位于(ESTabBarController.m) 这是我按下按钮时试图触发的: RecorderVi

我遇到的问题是,我希望通过单击UIView(页脚)中的按钮来调用navigationController中的方法。当我按下按钮时,它应该调用我试图访问的方法,以打开下面代码中的录像机

我被告知可以实现委托方法或使用NSNotification。以下是我所拥有的:

我的页脚(ESPhotoDetailsFooterView.m)有我创建的按钮。我的页脚只包含一个UIView

我试图在页脚中访问的方法位于(ESTabBarController.m)

这是我按下按钮时试图触发的:

RecorderViewController *viewController = [[RecorderViewController alloc] init];
    [viewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self.navController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self.navController pushViewController:viewController animated:NO];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:self.navController animated:YES completion:nil];
    });
我对目标C不熟悉,了解基本知识。我不知道我需要做什么来完成这一点。任何帮助都将不胜感激

按钮的代码如下所示:

// Create a standard UIButton programmatically using convenience method
    UIButton *camButton = [UIButton buttonWithType:UIButtonTypeCustom];

    // Set the location (x,y) and size (width,height) of the button
    camButton.frame = CGRectMake(9.0f, 8.0f, 35.0f, 35.0f);

    // Create UIImages from image resources in your application bundle
    // using convenience methods (no need to release)
    UIImage *normal = [UIImage imageNamed:@"BingComm"];
    UIImage *highlighted = [UIImage imageNamed:@"BingCommClick"];

    // Set the button's background to an image
    [camButton setBackgroundImage:normal forState:UIControlStateNormal];
    [camButton setBackgroundImage:highlighted forState:UIControlStateHighlighted];

    // Add the target-action for the touch event
    #pragma GCC diagnostic ignored "-Wundeclared-selector"

    [camButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.mainView addSubview:camButton];

是的,在这种情况下,委派是一个不错的选择,基本上在页脚视图中需要委派对象。然后在运行时将委托设置为TabBarController。 当用户单击按钮时,使用您的方法调用委派
[委派方法]

这是一个非常重要的设计模式objective-c,如果您能够按照本教程完全理解委托,这将非常有帮助


我同意授权很重要,非常值得学习,但解决同样问题的另一种方法是:

在btnClicked的方法定义中,调用以下代码

if([self tabBarController]) {
     [[self tabBarController] methodToCall];
}

由于视图控制器应嵌入在选项卡栏控制器中,因此它将设置此属性。然后可以调用tabBarController类中包含的任何公共方法

如何在故事板或代码中设置按钮?目前尚不清楚按钮的目标,即按下按钮时告知的对象(理想情况下为某种类型的视图控制器)是否根据上述内容正确设置。如果你提供更多的细节,我可能会让你从正确的方向开始。我在上面添加了我正在使用的按钮代码。