我是每个人,
我有3个按钮,每个按钮用“取消”和“确定”调用AlertView,每个“确定”按钮进入不同的视图。
现在我用这个
解决了这个问题- (UIButton *)1_BTN
{
if (1_BTN == nil)
{
UIImage *buttonBackground = [UIImage imageNamed:@"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:@""
target:self
selector:@selector(1_BTN:)
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
......
- (void)1_BTN:(NSInteger *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"some fancy text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ok", nil];
[alert setTag:[sender valueForKey:@"tag"]];
[alert show];
[alert release];
}
.......
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
something should happen.....
}
对于所有三个按钮,它工作正常,但
[alert setTag:[sender valueForKey:@"tag"]];
和
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
我收到此警告“无效的接收器类型”NSInteger“”
为什么会这样,我怎样才能更好地解决这个问题?
答案 0 :(得分:2)
首先,你完全错了。实施UIAlertViewDelegate
,特别是alertView:clickedButtonAtIndex:
并检查哪个按钮索引(来自0 .. n
,其中n
是最后一个按钮)。根据您已知的固定指数采取相应行动。
其次,NSInteger
是标量非对象类型,无法接收消息。您可能希望进行相等比较,即alertview.tag == 1
。但正如我之前所说,不要这样做。
答案 1 :(得分:1)
UIView属性“tag”不是一个对象,它只是一个简单的NSInteger。它与你可能从C / C ++中知道的“int”几乎相同。我在你的代码中纠正了几行。现在应该可以了。
另一件事:UIButton的配置对我来说似乎有点奇怪。也许你应该检查你的记忆管理。
- (UIButton *)1_BTN
{
if (1_BTN == nil) {
UIImage *buttonBackground = [UIImage imageNamed:@"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:@"" target:self selector:@selector(1_BTNAction:) frame:frame image:buttonBackground imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
- (void)1_BTNAction:(UIButton *)sender {
NSInteger tagNumber = [sender tag];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"sone fancy text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ok", nil];
[alert setTag:tagNumber];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([alertView tag] == 1) {
//something should happen
}
}
}