首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UIView中的按钮从另一个ViewController触发方法

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

Stack Overflow用户
提问于 2016-02-23 00:06:04
回答 2查看 250关注 0票数 0

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

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

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

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

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

代码语言:javascript
复制
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];
    });

我是Objective C的新手,了解基础知识。我不知道我需要做些什么才能做到这一点。任何帮助都将不胜感激。

该按钮的代码如下:

代码语言:javascript
复制
// 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];
EN

回答 2

Stack Overflow用户

发布于 2016-02-23 00:38:33

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

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

http://www.alexefish.com/post/522641eb31fa2a0015000002

票数 0
EN

Stack Overflow用户

发布于 2016-02-23 03:03:47

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

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

代码语言:javascript
复制
if([self tabBarController]) {
     [[self tabBarController] methodToCall];
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35558313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档