嗨我有多個UIButtons(foo
和bar
),並按下時,每個調用不同的實例方法(doSomethingFoo
和doSomethingBar
)。以下是我使用的代碼:如何在objective-c實例方法中傳遞字符串?
CGRect fooImageRect = CGRectMake(38.0f, 192.0f, 130.0f, 25.0f);
UIButton *buttonFoo = [[UIButton alloc] init];
buttonFoo.frame = fooImageRect;
[buttonFoo setImage:[UIImage imageNamed:@"button_foo_130x25.png"] forState:UIControlStateNormal];
[buttonFoo addTarget:self action:@selector(doSomethingFoo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonFoo];
[buttonFoo release];
CGRect barImageRect = CGRectMake(172.0f, 192.0f, 130.0f, 25.0f);
UIButton *buttonBar = [[UIButton alloc] init];
buttonBar.frame = barImageRect;
[buttonBar setImage:[UIImage imageNamed:@"button_bar_130x25.png"] forState:UIControlStateNormal];
[buttonBar addTarget:self action:@selector(doSomethingBar:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonBar];
[buttonBar release];
和實例方法doSomethingFoo
和doSomethingBar
-(void)doSomethingFoo:(id) sender {
// code doing something with a NSString having value 'foo'
}
-(void)doSomethingBar:(id) sender {
// same code doing something with a NSString having value 'bar'
}
我所試圖做的是建立一個單一的方法(doSomething
),我可以調用當任按鈕,並將它傳遞給我將在方法中引用的字符串(值'foo'或'bar')。我正在努力處理語法。