[英]Passing data to the ViewController issue
我的任務是通過單擊FirstViewController上的按鈕在SecondViewController的UIImageView中設置背景圖像。
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中的button方法:
- (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
返回-這是SecondViewController中的IBOutlet UIImageView。 另外,我在SecondViewController.m中導入了FirstViewController.h。 我試圖將字符串傳遞給SecondViewController-並在SecondViewController的-ViewDidLoad中將其記錄為正常,但無法設置UIImageView。
我想,問題在於,當視圖加載到模態控制器中時,首先要通過屬性設置UIImageView,然后再通過xib對其進行設置。
你可以做這樣的事情。 您可以在secondviewcontroller.h中有一個UIImageView出口。
IBOutlet UIImageView *imgView;
也屬性並合成這個
@property(nonatomic, retain) IBOutlet UIImageView *imgView;
連接FirstViewController.xib中的secondViewController的出口iof
在您的firstviewcontrollem.m中實現此功能,在第二個ViewController UIImageView中設置圖像的位置
self.secondViewData.imgView.image = [UIImage imageNamed:@"1.png"];
這行是問題所在:
svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
您將重新設置為新分配的圖像視圖,而不是IB中連接的圖像視圖。 只需將該行更改為:
svc.back.image = [UIImage imageNamed:@"1.png"]];
您需要在第二個視圖控制器處獲取另一個imageview。我命名為imgView。原始圖像視圖為bgView。
enter code here
//在secondviewcontroller.h
@property(nonatomic,strong)IBOutlet UIImageView * bgView;
@property(nonatomic,strong)IBOutlet UIImageView * imgView;
//在secondviewcontroller.m
//在viewDidLoad
self.bgView.image = self.imgView.image;
//在firstviewcontroller.m
//按鈕動作應該像這樣
-(IBAction)buttonPressed:(id)發件人{
SecondViewController *secondVC=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
secondVC.imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hue.png"]];
[self presentModalViewController:secondVC animated:YES];
}
嘗試這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.