[英]Can't send data between views using a protocol & delegate with presentModalViewController
我在做电梯东西。 我在使用presentModalViewController发送具有不同视图的数据时遇到问题。 我没有找到红色消息“ favoriteColorString”属性。 我复制了完全相同但不同的表单名称和按钮。 “ favoriteColorString”出现错误,无法发送电梯2数据。
我尝试了两种不同的方法。
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
电梯视图
#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
“找不到属性”的原因是您将ivar命名为与类相同。 点表示法只是一种语法糖: object.property = value
等效于[object setProperty:value]
。 在Objective C中,类也是对象,并且当您调用Elevator2View.favoriteColorString = whatever
setFavoriteColorString
时,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:
。 当然,这个全新的 Elevator2View
对象不知道Elevator2View
的(您的实例变量的)属性是什么!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.