2012-11-12 106 views
0

我的任务是通过单击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。

+1

你是什么意思“back-它是在SecondViewController中的UIImageView”?是回IBOutlet。如果没有,那么在SecondViewController中查看与“返回”有关的任何代码会很有帮助。 – rdelmar

+0

在视图之间传输数据的好方法 - http://iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-and-other-objects-link-fixed.html – ignotusverum

+1

这是一个ARC项目?是否在SecondViewController的xib中设置?即使你用实用的方式设置它,当视图在svc中加载任何默认值时,它都会被覆盖。最好的方法可能是将图像作为自定义init方法中的值传递,并将其加载到svc viewDidLoad方法中。 – propstm

回答

1

这条线的问题是:

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

你设置恢复到新分配的图像视图,而不是它挂接到IB的一个。只要改变该行:

svc.back.image = [UIImage imageNamed:@"1.png"]]; 
1

你可以做这样的事情。 您可以的UIImageView的出口在secondviewcontroller.h

 IBOutlet UIImageView *imgView; 

也财产和合成这种

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

在FirstViewController.xib

连接出口IOF secondViewController在你firstviewcontrollem .m在你想要在第二个控制器中设置图像的地方实现这个UIImageView

self.secondViewData.imgView.image = [UIImage imageNamed:@"1.png"]; 
1

ü需要采取的名为imgView.Let原始图像视图bgView第二视图controller.I另一个ImageView的。

enter code here 

//在secondviewcontroller.h

@属性(非原子,强)IBOutlet中的UIImageView * bgView;

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

//在secondviewcontroller.m

//在viewDidLoad中

self.bgView.image = self.imgView.image;

//在firstviewcontroller。米

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

- (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]; 

}

尝试此。

相关问题