我的任务是通过单击FirstViewController上的按钮在SecondViewController中的UIImageView中设置背景图像。将数据传递给ViewController问题
FirstViewController.h:
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@class SecondViewController;
@interface ViewController : UIViewController
{
SecondViewController *secondViewData;
}
// pointer to second view
@property (nonatomic, strong) SecondViewController *secondViewData;
// buttons
- (IBAction)changeBack:(id)sender;
@end
按钮方法FirstViewController.m:
- (IBAction)changeBack:(id)sender {
SecondViewController *svc = [[SecondViewController alloc] init];
self.secondViewData = svc;
svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:svc animated:YES completion:nil];
}
SecondViewController.h:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface SecondViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *back;
@end
回来 - 这是一个IBOutlet中的UIImageView在SecondViewController。 另外我在SecondViewController.m中导入了FirstViewController.h。 我试图将字符串传递给SecondViewController - 它在SecondViewController中的-ViewDidLoad中记录的很好,但无法设置UIImageView。
你是什么意思“back-它是在SecondViewController中的UIImageView”?是回IBOutlet。如果没有,那么在SecondViewController中查看与“返回”有关的任何代码会很有帮助。 – rdelmar
在视图之间传输数据的好方法 - http://iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-and-other-objects-link-fixed.html – ignotusverum
这是一个ARC项目?是否在SecondViewController的xib中设置?即使你用实用的方式设置它,当视图在svc中加载任何默认值时,它都会被覆盖。最好的方法可能是将图像作为自定义init方法中的值传递,并将其加载到svc viewDidLoad方法中。 – propstm