[英]Pass data through views Xcode 4.2 storyboarding
我的項目基本上是高性能汽車應用程序,使用arc4rand()
確定下一步將顯示哪種汽車。 我做了一張圖片來解釋我需要做什么。
我的.h代碼
@interface BMW : UIViewController {
IBOutlet UIImageView *view;
IBOutlet UILabel *label;
}
-(IBAction)random;
@end
我的.m代碼
-(IBAction)random {
int image = arc4random() % 1;
switch (image) {
case 0:
block.image = [UIImage imageNamed:@"m3.jpg"];
label.text = @"BMW m3" ;
break;
default:
break;
}
我如何獲取標簽和圖片並保留到第二視圖? 我正在使用Xcode 4.2.1和情節提要
一種可能性:在第二個視圖控制器中創建標簽和圖片ivars,並在newController.h
像這樣設置它們
@interface newController : UIViewController
@property (strong, nonatomic) NSString *labelText;
@property (strong, nonatomic) UILabel *label;
@property (strong, nonatomic) UIImage *image;
@end
在newController.m
@implementation newController
@synthesize labelText, image, label;
- (void)viewWillAppear
{
label.text = labelText;
}
@end
將IB / Xcode中的segue的標識符設置為pushNewViewController
,並在代碼中添加prepareForSegue:
#import "NewController.h"
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"pushNewController"]) {
[segue.destinationViewController setLabelText:label.text];
[segue.destinationViewController setImage:block.image];
}
}
如果您想了解有關UIViewControllers
和情節提要的更多信息,我建議您查看當前的Stanford CS 193P課程,該課程可與iOS 5完全兼容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.