0

嗨,我是 iPhone Xcode 开发的新手,一直坚持实现历史功能。

用外行的话来说,我有

4 个按钮(每个按钮都有自己独特的图像)

(按钮 1) (按钮 2) (按钮 3) (按钮 4)

和 5 个图像视图

(imageview 位置 1) (imageview 位置 2) (imageview 位置 3) (imageview 位置 4) (imageview 位置 5)

每次单击按钮(最多 5 次)时,我都需要将其显示在适当的图像视图中。

即,如果首先按下按钮 4,它将在图像视图 1 中显示,如果第二次按下按钮 3,它将在图像视图 2 中显示,依此类推。

我真的很感激一些帮助或朝着正确的方向前进。

提前致谢


更新代码:

viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {


IBOutlet UIImageView *imageview_history_1;
IBOutlet UIImageView *imageview_history_2;
IBOutlet UIImageView *imageview_history_3;
IBOutlet UIImageView *imageview_history_4;
IBOutlet UIImageView *imageview_history_5;

}

// I would think this could be consolidated into one function
-(IBAction)showhistory_1;
-(IBAction)showhistory_2;
-(IBAction)showhistory_3;
-(IBAction)showhistory_4;
-(IBAction)showhistory_5; 

@end



#import ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



// function to add to history
// Need an if/for statement???
// On button event 1 display result at image view_history 1, 
// On button event 2 display result at image view_history 2.

- (IBAction)showhistory_1 {
UIImage *img = [UIImage imageNamed:@"button1"];

[imageview_history_1 setImage:img];

}


@end

//我一直在创建函数。再次感谢。

4

3 回答 3

1

首先,在你的xib中,给每个按钮一个标签号以便于识别它:
假设你给button1一个标签1,button2是标签2,等等。

然后,我会将您的 UIButton 操作更改为全部指向相同的功能:

。H:

- (IBAction)show_history:(UIButton)sender;

米:

- (void)setNextHistoryImage:(UIImage *)img {
    if (imageview_history_1.image == nil) {
        imageview_history_1.image = img;
    } else if (imageview_history_2.image == nil) {
        imageview_history_2.image = img;
    }  else if (imageview_history_3.image == nil) {
        imageview_history_3.image = img;
    }  else if (imageview_history_4.image == nil) {
        imageview_history_4.image = img;
    }  else if (imageview_history_5.image == nil) {
        imageview_history_5.image = img;
    }
}

- (IBAction)show_history:(UIButton)sender {
    NSString *imgName = [NSString stringWithFormat:@"button%d", sender.tag];
    UIImage *img      = [UIImage imageNamed:imgName];
    [self setNextHistoryImage:img];
}
于 2012-05-05T05:43:29.157 回答
0

你可以这样做:

-(IBAction)showhistory_1:(id)sender
{

    if(sender == firstbutton)
    {
       UIImage *img = [UIImage imageNamed:@"button4"];
       [imageview_history_4 setImage:img];
    }
    else if(sender == secondbutton)
    {
       UIImage *img = [UIImage imageNamed:@"button3"];
       [imageview_history_3 setImage:img];
    }
    else if(sender == thirdbutton)
    {
      UIImage *img = [UIImage imageNamed:@"button2"];
      [imageview_history_2 setImage:img];
   }
   else if(sender == fourthbutton)
   {
      UIImage *img = [UIImage imageNamed:@"button1"];
      [imageview_history_1 setImage:img];
   }
}
于 2012-05-05T05:26:54.157 回答
0

做一件事,只需更改按钮单击时 imageview 的位置。像这样使用并尝试它的工作是否通知我们,如果不是,我还将提供更多方法。

  ImageView.frame=CGRectMake(0, 0, 305, 135);

使用不同的 2 位置。

于 2012-05-05T05:44:22.203 回答