无法使用协议和视频在视图之间发送数据。使用presentModalViewController委托

时间:2013-04-10 18:22:54

标签: ios protocols appdelegate

我正在制作电梯。我使用presentModalViewController在使用不同视图发送数据时遇到问题。我找不到红色消息“favoriteColorString”属性。我复制了完全相同但不同的表单名称和按钮。 “favoriteColorString”出现错误,无法发送elev2数据。

我尝试了两件不同的事情。

  Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

  favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

这是我的代码:

ElevatorView.h

#import <UIKit/UIKit.h>
#import "Elevator2View.h"

@interface ElevatorView : UIViewController<PassSecondColor>
 {

Elevator2View   *Elevator2View;

IBOutlet UITextField     *favoriteColorTextField;
IBOutlet UILabel         *favoriteColorLabel;
IBOutlet UILabel         *secondFavoriteColorLabel;

NSString        *secondFavoriteColorString;

}

@property (nonatomic, retain) Elevator2View *Elevator2View;
@property (nonatomic, retain) IBOutlet UITextField  *favoriteColorTextField;
@property (nonatomic, retain) IBOutlet UILabel      *favoriteColorLabel;
@property (nonatomic, retain) IBOutlet UILabel      *secondFavoriteColorLabel;

@property (copy) NSString   *secondFavoriteColorString;

@end

ElevatorView.m

#import "ElevatorView.h"
#import "Elevator2View.h"
    @implementation ElevatorView
@synthesize Elevator2View, favoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
@synthesize secondFavoriteColorString;



-(IBAction)level1:(id)sender;{
   favoriteColorTextField.text = @"1";
      Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

      [self presentModalViewController:[[[Elevator2View alloc] init]
                                  autorelease] animated:NO];
}

Elevator2View.h

#import <UIKit/UIKit.h>

@protocol PassSecondColor <NSObject>
@required
- (void) setSecondFavoriteColor:(NSString *)secondFavoriteColor;
@end

@interface Elevator2View : UIViewController{
IBOutlet UITextField *secondFavoriteColorTextField;
IBOutlet UILabel     *favoriteColorLabel;
IBOutlet UILabel     *secondFavoriteColorLabel;
NSString             *favoriteColorString;
id <PassSecondColor> delegate;

}

@property (copy) NSString   *favoriteColorString;



@property (nonatomic, retain) IBOutlet UITextField  *secondFavoriteColorTextField;
@property (nonatomic, retain) IBOutlet UILabel      *favoriteColorLabel;
@property (nonatomic, retain) IBOutlet UILabel      *secondFavoriteColorLabel;
@property (retain) id delegate;

@end

Elevator2View.m

#import "Elevator2View.h"

@interface Elevator2View ()

@end

@implementation Elevator2View
@synthesize secondFavoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
@synthesize favoriteColorString;
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       // Custom initialization
   }
   return self;
}

- (void) viewWillAppear:(BOOL)animated
{
    favoriteColorLabel.text = favoriteColorString;  
}

- (void) viewWillDisappear:(BOOL) animated
{
//  [[self delegate] setSecondFavoriteColor:secondFavoriteColorTextField.text];
}


- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
    favoriteColorLabel.text = favoriteColorString;  
}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

请参阅http://www.theappcodeblog.com/?p=90

1 个答案:

答案 0 :(得分:0)

您的“未找到财产”的原因是您将您的ivar命名为与班级相同。 点符号只是一种语法糖:object.property = value等同于[object setProperty:value]。在Objective C中,类也是对象,当您调用Elevator2View.favoriteColorString = whatever时,Xcode显然认为您正在尝试调用Elevator2View类的方法setFavoriteColorString

轻松解决此错误:只需将您的ivar Elevator2View *Elevator2View重命名为其他内容即可。实际上,Xcode 4.4和更新的自动合成了属性的ivars:如果你有一个属性propertyName,那么Xcode将自动合成ivar _propertyName。您的财产Elevator2View将有_Elevator2View ivar。因此,除非您真的需要具有不同命名方案的ivars,否则您可以删除@synthesize,并且您也不需要为您的属性声明ivars。

(虽然我更喜欢为属性声明ivars(遵循Xcode命名方案),因为lldb常常不显示自动合成 - 在对象检查器中没有声明ivars。)

那是关于属性,ivars和命名约定。但是你在这段代码中做了什么?

-(IBAction)level1:(id)sender;{
   favoriteColorTextField.text = @"1";
      Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

      [self presentModalViewController:[[[Elevator2View alloc] init]
                                  autorelease] animated:NO];
}

您设置Elevator2View的值 - 您的实例变量的 - 属性,然后创建Elevator2View 的全新对象,将 作为模态视图控制器。 (顺便说一句,在iOS 6.0中不推荐使用presentModalViewController:animated:)。当然,这个品牌 new Elevator2View对象不知道Elevator2View(您的实例变量)的属性是什么!