2013-03-08 155 views
0

問題是,我在viewcontroller(ViewController)上有9個按鈕,並且我有一個用於存儲所有這些按鈕插座的插座集合。從視圖控制器發送信息到另一個

然後我有一個操作方法來處理這些按鈕的點擊事件。

我要的是發送按鈕圖像背景(作爲一個的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點擊,它老是顯示爲「6」在vc2 ... 有什麼建議嗎?感謝

+0

爲什麼'for'循環覆蓋'self.image'? – trojanfoe 2013-03-08 16:25:21

回答

1

問題是這樣的:

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

這將在self.cardBtns設置self.image到最後一個項目。你希望將self.image設置爲sender.currentBackgroundImage(並將參數類型中的id更改爲UIButton *),然後消除循環。

相關問題