質問

I'm trying to assign two actions to one BarButtonItem but I have a problem with the syntax (bad receiver type 'NSInteger' aka 'long') and I cannot compile my app. Here is the wrong code:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction1)];
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction2)];

I'm using Xcode 5.1 and my target is iOS 7.0.

Can you help me? Thank you in advance.

役に立ちましたか?

解決 3

it that really your code ?

initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction2)];

isn't the receiver missing there ?

also a UIBarButtonItem can only have one target, if you want two actions getting called create a third one that will call the two others

- (IBAction)myAction3:(id)sender {
  [self myAction1:sender];
  [self myAction2:sender];
}

他のヒント

You cannot assign two actions to a BarButtonItem.

You can either call the second action in the selector method.

Or you can simply remove previous target and add new target action conditionally during runtime.

You can not add multiple target/actions to a UIBarButtonItem.

You can add a bar button item with one target/action like so:

UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction1)];
self.navigationItem.leftBarButtonItem = myBarButtonItem;

You'll then need to do something like call you second method from the one specified, or whatever is appropriate for your situation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top