在 UIbutton 中分配随机标题
Posted
技术标签:
【中文标题】在 UIbutton 中分配随机标题【英文标题】:Assign random title in UIbutton 【发布时间】:2012-11-10 10:16:44 【问题描述】:我的 application
与测验有关,在我看来,我有 1 个图像视图和 4 个 UIbuttons
,最初我必须在 imageview
中加载图像并以随机方式分配 4 个按钮标题,其中一个按钮应该是当前图像名称,如果用户单击按钮我想显示下一组按钮标题和新的image
-(IBAction)answersetting:(id)sender
int i=0;
UIButton *mybutton = (UIButton *)sender;
[mybutton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
for(int loop = 0; loop<4;loop++)
animage.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[imageary objectAtIndex:i]]];
name=[self convertToDisplayName:[imageary objectAtIndex:i]];
[mybutton setTitle:name forState:UIControlStateNormal];
NSLog(@"current image name :%@",name);
if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:i]])
//titles equal
[self alertshow];
i++;
UIView *newView = [self.view viewWithTag:i];
if([newView isKindOfClass:[UIButton class]])
UIButton *newButton = (UIButton *)newView;
[newButton setTitle:name forState:UIControlStateNormal];
这无法执行我上面描述的内容。我应该对我的代码进行哪些更改?请帮助解决这个问题
【问题讨论】:
你使用了我对你上一个问题的回答是对的 :) ***.com/questions/13284405/… @MidhunMP 我用过,但只有当我点击时才会显示标题,并且所有按钮标题都相同 无需一次又一次地提出相同的问题,这不会为您提供任何答案,哈哈 :) 我只是任何其他选项的平均值@Wolvorin 但是如果有人有任何问题,他们可以在同一个问题上给出它,你不需要再次发布它作为不同的问题:) 【参考方案1】:只需创建一个显示图像和设置按钮标题的方法。并使用另一种方法来评估猜测结果。
//This method display the image and buttons
-(void)displayImageAndButton:(int)pos
animage.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[imageary objectAtIndex:pos]]];
UIView *newView = [self.view viewWithTag:buttonTag1];
if([newView isKindOfClass:[UIButton class]])
UIButton *newButton = (UIButton *)newView;
name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
newView = [self.view viewWithTag:buttonTag2];
if([newView isKindOfClass:[UIButton class]])
UIButton *newButton = (UIButton *)newView;
name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
newView = [self.view viewWithTag:buttonTag3];
if([newView isKindOfClass:[UIButton class]])
UIButton *newButton = (UIButton *)newView;
name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
newView = [self.view viewWithTag:buttonTag4];
if([newView isKindOfClass:[UIButton class]])
UIButton *newButton = (UIButton *)newView;
name=[self convertToDisplayName:[imageary objectAtIndex:pos++]];
[newButton addTarget:self action:@selector(submitGuess:)
forControlEvents:UIControlEventValueChanged];
[newButton setTitle:name forState:UIControlStateNormal];
//this method evaluates the answer
- (IBAction)submitGuess:(id)sender
UIButton *mybutton = (UIButton *)sender;
if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:positionOfQuest]])
//titles equal
[self alertshow];
positionOfQuest++;
[self displayImageAndButton:positionOfQuest];
这里 buttonTag1,buttonTag2,buttonTag3 & buttonTag4 是你的按钮的标签。
int viewDidLoad
声明一个变量,如:
static positionOfQuest = 0; //this for holding the question number
在IB中添加按钮标签并在viewDidLoad
中初始化它们,然后调用以下方法
你需要调用viewDidLoad
中的方法,比如:
[self displayImageAndButton:positionOfQuest];
【讨论】:
【参考方案2】:如果你喜欢简单的方法...
#include <stdlib.h>
NSMutableArray *quiz = [NSMutableArray alloc....];
for (int i=0; i<4; i++)
int r = arc4random() % [quiz count];
NSLog(@"%@", [quiz objectAtIndex:r]);
_Button_1.title = [quiz objectAtIndex:r];
// or better, i suggest you to use KVC
// [self valueForKey:@"_Button_%d", i] setTitle //something like this.
//
【讨论】:
以上是关于在 UIbutton 中分配随机标题的主要内容,如果未能解决你的问题,请参考以下文章