iOS无法从不同的视图控制器向按钮添加操作

时间:2016-11-22 10:02:58

标签: ios objective-c

我有一个ViewController 1,其中有一个按钮,使用当前视图控制器调用ViewController 2。我在按钮ViewController 2中添加了一个动作。但Button在点击时不会调用该操作。 我的代码是:

显示ViewController 2

- (void)viewDidLoad{
    [super viewDidLoad];

    ViewController2 * addShot = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
    [addShot.addShotButton addTarget:self action:@selector(submitShot:) forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)addShotButtonTapped:(id)sender{
    [addShot setModalPresentationStyle:UIModalPresentationPopover];
    [addShot setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:addShot];
    [self presentViewController:nav animated:YES completion:nil];
}

一切正常但submitShot action永远不会被调用。建议?

3 个答案:

答案 0 :(得分:1)

在ViewController2.h中添加协议,如下所示

@protocol  ViewController2Delegate <NSObject>

-(void) submitShotTapped;

@end
@interface

之前

并在您的界面中声明它的属性。

@property (weak, nonatomic) id<ViewController2Delegate> delegate;

始终为您的委托使用属性。

在你的submitShot的动作上添加调用这个委托方法,如下所示

- (IBAction) submitShot:(id)sender {
    [self.delegate submitShotTapped];
}

现在,你的ViewController1声明了这样的委托

@interface TagDetails ()<ViewController2Delegate>

现在,在创建ViewController2的对象时,将此委托分配给ViewController1的self,就像这样

ViewController2 * addShot = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
addShot.delegate = self;

然后在ViewController1中实现该委托方法,就像这样

-(void) submitShotTapped {
     //Shot button tapped on ViewController2
}

这是你如何使用委托,也删除你在按钮上添加的目标。

答案 1 :(得分:0)

这是错误的,你正在创建另一个MyPtojectLib.dll MyProjectLib.ilk MyProjectLib.pdb 的实例,它不在你的屏幕上,基本上什么都不做:

ViewController2

应该是:

ViewController2 * addShot = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

[addShot.addShotButton addTarget:self action:@selector(submitShot:) forControlEvents:UIControlEventTouchUpInside];

答案 2 :(得分:0)

你在做什么是错误的方式。尝试以下代码。这应该工作。

<强> ViewControler2.h

 @interface ViewControler2  
    //your other stuff I guess
    @property (strong , nonatomic) UIButton *addShotButton;
  @end

<强> ViewControler2.m

 @implementation ViewController2

    @synthesize addShotButton;

    - (void)viewDidLoad{
        [super viewDidLoad];
        [self.addShotButton addTarget:self action:@selector(submitShot:) forControlEvents:UIControlEventTouchUpInside];    
    }
    -(void)submitShot:(id)sender{
     //Your action
    }
    @end

ViewController1.h

@interface ViewController1
@end

ViewController1.m

@implementation ViewController1
- (void)viewDidLoad{
    [super viewDidLoad];
}

- (IBAction)addShotButtonTapped:(id)sender{
    ViewController2 * addShot = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
    [addShot setModalPresentationStyle:UIModalPresentationPopover];
    [addShot setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:addShot];
    [self presentViewController:nav animated:YES completion:nil];
}
@end