0

问题是,我在 viewcontroller (ViewController) 上有 9 个按钮,并且我有一个用于存储所有这些按钮插座的插座集合。

然后我有一个操作方法来处理这些按钮的点击事件。

我想要的是发送 CLICKED 按钮的按钮图像背景(作为 UIImage * img),segue 到另一个视图控制器(vc2),

这是我的代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardBtns;


- (IBAction)cardAction:(id)sender;

@end




#import "ViewController.h"
#import "ResultViewController.h"

@interface ViewController ()
@property(strong,nonatomic)UIImage *image;
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)cardAction:(id)sender {
for(UIButton *cardButton in self.cardBtns){
    self.image = [cardButton currentBackgroundImage];
}
[self performSegueWithIdentifier:@"cardSegue" sender:self.image];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//UIImage * img = [UIImage imageNamed:@"f1"];
UIImage * img = (UIImage *)sender;

ResultViewController *viewcontroller = [segue destinationViewController];
viewcontroller.img = img;
}

@end

在 vc2 中,我在这里有另一个按钮,我希望用刚刚从 vc1 segue 发送的 img 绘制这个按钮背景 img。

@interface ResultViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *cardBtn;
@property(strong,nonatomic)UIImage *img;

- (IBAction)cardDismiss:(id)sender;

@end



#import "ResultViewController.h"

@interface ResultViewController ()

@end

@implementation ResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self.cardBtn setImage:self.img forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)cardDismiss:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
}
@end

假设我的按钮是 1,2,3,4,5,6,7,8,9

现在,无论我在 vc1 中单击什么按钮,它总是在 vc2 中显示为“6”......有什么建议吗?谢谢

4

1 回答 1

1

问题是这样的:

- (IBAction)cardAction:(id)sender {
for(UIButton *cardButton in self.cardBtns){
    self.image = [cardButton currentBackgroundImage];
}
[self performSegueWithIdentifier:@"cardSegue" sender:self.image];
}

This will set self.image to the last item in self.cardBtns. You want to set self.image to sender.currentBackgroundImage (and change id in the argument type to UIButton *), and just eliminate the loop.

于 2013-03-08T16:28:30.883 回答