我的应用程序
就像与测验相关,在我看来,我有1个 ImageView 和4个UI按钮
,最初我必须在imageview<中加载图像
并以随机方式分配4个按钮标题,其中一个按钮应该是当前图像名称,如果用户单击该按钮我想显示下一组按钮标题和新图像
-(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];
}
}
}
}
这无法执行我上面描述的内容。我应该在代码中进行哪些更改?请帮助解决这个问题
最佳答案
只需创建一个用于显示图像和设置按钮标题的方法。并使用另一种方法来评估猜测结果。
//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];
关于iphone - 在 UIbutton 中分配随机标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13321143/