【问题标题】:Passing data to the ViewController issue将数据传递给 ViewController 问题
【发布时间】:2012-11-01 02:21:04
【问题描述】:

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

【问题讨论】:

  • “back-它是 SecondViewController 中的 UIImageView”是什么意思?又回来了一个 IBOutlet。如果没有,那么在 SecondViewController 中查看与“返回”有关的任何代码会很有帮助。
  • 在视图之间传输数据的好方法 - iphonedevsdk.com/forum/iphone-sdk-development/…
  • 这是一个 ARC 项目吗?在 SecondViewController 的 xib 中重新设置了吗?即使您务实地进行设置,当视图在 svc 中加载时,它也会被任何默认值覆盖。最好的方法可能是将图像作为值传递到自定义 init 方法中,然后在 svc viewDidLoad 方法中加载。
  • 我建议不要为back设置图像视图,而是为图像(不是图像视图)设置一个属性,并让SecondViewControllerviewDidLoad设置图像视图以使用图像您在此处设置的属性。一般来说,你不应该在viewDidLoad 之前搞乱控件,因为视图控制器的视图可能还没有创建。
  • rdelmar,是的,它是一个 IBOutlet

标签: ios objective-c iphone xcode


【解决方案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;

//在firstviewcontroller.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];

}

试试这个。

【讨论】:

    【解决方案2】:

    这行是问题所在:

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

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

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

    【讨论】:

      【解决方案3】:

      首先,阅读Resource Management in View Controllers

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

      【讨论】:

        【解决方案4】:

        你可以做这样的事情。 你可以在你的 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"];
        

        【讨论】:

          猜你喜欢
          • 2018-03-21
          • 1970-01-01
          • 2019-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多