1

我的任务是通过单击 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 中的按钮方法:

- (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。

4

4 回答 4

2

首先,阅读View Controllers 中的资源管理

我想,问题是首先你通过属性设置 UIImageView,然后在模式控制器中加载视图时从 xib 设置它。

于 2012-11-12T17:20:14.563 回答
1

你可以做这样的事情。你可以在你的 secondviewcontroller.h 中有一个 UIImageView 的出口

        IBOutlet UIImageView *imgView;

也属性和合成这个

       @property(nonatomic, retain)  IBOutlet UIImageView *imgView;  

连接FirstViewController.xib中secondViewController的outlet iof

在你的 firstviewcontrollem.m 中实现这个你想在 secondViewController UIImageView 中设置图像的地方

    self.secondViewData.imgView.image = [UIImage imageNamed:@"1.png"];
于 2012-11-12T17:19:10.183 回答
1

这条线是问题所在:

svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];

您正在设置回一个新分配的图像视图,而不是它在 IB 中连接的那个。只需将该行更改为:

svc.back.image = [UIImage imageNamed:@"1.png"]];
于 2012-11-12T17:29:21.547 回答
1

你需要在第二个视图控制器上获取另一个图像视图。我命名为 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;

//首先viewcontroller.m

//按钮动作应该是这样的

-(IBAction)buttonPressed:(id)sender {

SecondViewController *secondVC=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

secondVC.imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hue.png"]];

[self presentModalViewController:secondVC  animated:YES];

}

试试这个。

于 2012-11-14T14:29:42.190 回答