Iphone 有史以来最奇怪的事情,UIButton@selector检测到右键,做错了';如果';,还有其他原因吗;?
因此,我动态创建了3个UIButton(目前),其中包含以下循环:Iphone 有史以来最奇怪的事情,UIButton@selector检测到右键,做错了';如果';,还有其他原因吗;?,iphone,objective-c,uibutton,selector,Iphone,Objective C,Uibutton,Selector,因此,我动态创建了3个UIButton(目前),其中包含以下循环: NSMutableArray *sites = [[NSMutableArray alloc] init]; NSString *one = @"Constution Center"; NSString *two = @"Franklin Court"; NSString *three = @"Presidents House"; [sites addObject: one];
NSMutableArray *sites = [[NSMutableArray alloc] init];
NSString *one = @"Constution Center";
NSString *two = @"Franklin Court";
NSString *three = @"Presidents House";
[sites addObject: one];
[one release];
[sites addObject: two];
[two release];
[sites addObject: three];
[three release];
NSString *element;
int j = 0;
for (element in sites)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//setframe (where on screen)
//separation is 15px past the width (45-30)
button.frame = CGRectMake(a, b + (j*45), c, d);
[button setTitle:element forState:UIControlStateNormal];
button.backgroundColor = [SiteOneController myColor1];
[button addTarget:self action:@selector(showCCView:)
forControlEvents:UIControlEventTouchUpInside];
[button setTag:j];
[self.view addSubview: button];
j++;
}
@Selector方法如下所示:
- (void) showCCView:(id) sender {
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSString* myNewString = [NSString stringWithFormat:@"%d", whichButton];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor whiteColor];
UINavigationBar *cc = [SiteOneController myNavBar1:@"Constitution Center Content"];
UINavigationBar *fc = [SiteOneController myNavBar1:@"Franklin Court Content"];
UINavigationBar *ph = [SiteOneController myNavBar1:@"Presidents House Content"];
if (whichButton = 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
else if (whichButton = 1)
{
NSLog(myNewString);
[self.view addSubview:fc];
}
else if (whichButton = 2)
{
NSLog(myNewString);
[self.view addSubview:ph];
}
}
现在,它正在将正确的按钮标记打印到NSLog,如方法所示,但是每个按钮都显示一个导航栏,标题为“Franklin Court”,每个按钮,即使我单击按钮0时,控制台上显示“按钮0已单击”,但仍执行else if(whichButton=1)代码
我在这里遗漏了什么吗?您在您的条件中使用的是
=
运算符,而不是=
运算符,这使得赋值而不是比较
由于赋值的返回值是赋值,因此首先
whichButton
变为0
,计算结果为false
,然后whichButton
变为1,计算结果为true,无论你做什么,你最终都会选择富兰克林法庭。你有一个问题,你应该使用==
而不是=
与此相反:
if (whichButton = 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
...
试试这个:
if (whichButton == 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
else if (whichButton == 1)
{
NSLog(myNewString);
[self.view addSubview:fc];
}
else if (whichButton == 2)
{
NSLog(myNewString);
[self.view addSubview:ph];
}
或者这个:
NSLog (myNewString); //occurs in all cases.
switch (whichButton)
{
case 0:
[self.view addSubview:cc];
break;
case 1:
[self.view addSubview:fc];
break;
case 2:
[self.view addSubview:ph];
break;
default:
// optionally handle the case where the button's tag was not 0, 1, or 2.
}
这就是我提升if(0==whichButton)编码风格的原因,因为编译器将捕获error@sbooth:这就是我提升只在分支结构中接受布尔表达式的语言的原因。
constant==variable
的问题在于它的可读性不好。这就像说“如果天空是蓝色的”或“如果男人是高大的”。问题是,我认为Xcode的默认编译器设置禁用了赋值警告,因为if(self=[super init])
习惯用法。@dreamlax:这是正确的。我们应该称之为比较风格的“尤达条件”。@zsneak:我赞成。