我是 iOS 开发的新手。我当前的任务是单击按钮更改图像。屏幕将由 4-5 个图像组成,单击按钮应更改 4-5 个图像之一。请指导我如何进行。
提前致谢。
问候, Bhaskar M.
在您的 .h 文件中声明一个数组 say imageArray
。
在 .m 文件中viewDidLoad
使用以下内容:
- (void)viewDidLoad
{
[super viewDidLoad];
imageArray=[[NSMutableArray alloc]init];
[imageArray addObject:@"mage1.png"];
[imageArray addObject:@"mage2.png"];
[imageArray addObject:@"mage3.png"];
[imageArray addObject:@"mage4.png"];
[imageArray addObject:@"mage5.png"];
[imageArray addObject:@"mage6.png"];
[imageArray addObject:@"mage7.png"];
}
在您的按钮操作中使用以下
-(IBAction) buttonPushed:(id)sender{
int x = arc4random() % 7;
//MyImageView will be the one you need to change
//you can choose it randomly. It's up to you
[MyImageView setImage:[UIImage imageNamed:[NSString stringwithformat:@"%@",[imageArray objectAtIndex:x]]]];
//[MyImageView setImage:[UIImage imageNamed:[imageArray objectAtIndex:x]]];
}
您应该首先创建一个在您按下按钮时会触发的方法。然后做这样的事情
-(void) buttonPushed{
//MyImageView will be the one you need to change
//you can choose it randomly. It's up to you
[MyImageView setImage:[UIImage imageNamed:@"qwe.jpg"]];
}