Iphone 目标C从一个视图切换到另一个视图
我对objective C编程还不熟悉,现在我需要快速创建一个iPhone应用程序。 我使用的是XCode 4.2 我在将NSString变量从一个视图传输到另一个视图时遇到问题。 这两个视图位于两组不同的.h和.m类中 在第一节课上,我有这样的东西Iphone 目标C从一个视图切换到另一个视图,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我对objective C编程还不熟悉,现在我需要快速创建一个iPhone应用程序。 我使用的是XCode 4.2 我在将NSString变量从一个视图传输到另一个视图时遇到问题。 这两个视图位于两组不同的.h和.m类中 在第一节课上,我有这样的东西 @interface firstview : UIViewController { NSString *test; } -(IBAction)testbutton @end 在firstview的.m范围内 -(IBAction)testbut
@interface firstview : UIViewController {
NSString *test;
}
-(IBAction)testbutton
@end
在firstview的.m范围内
-(IBAction)testbutton{
secondView *second;
[second setText:text]; //set text is a function that will take an NSString parameter
second= [[secondView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
在我写的第二个视图的.h中
@interface secondView : UIViewController{
-IB
}
您的想法是正确的,但您试图在
秒
指向有效对象之前调用-setText:
!改为这样做:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
另外,您为secondView
类提供的接口看起来既不正确又不完整--我不确定您想用-IB
部分做什么。如果您遵循通常的Objective-C命名约定,并以大写字符开始类名:SecondView
而不是SecondView
,这将对将来有所帮助。最后,我建议不要将视图控制器命名为“…视图”,因为这样很容易将视图控制器与UIView混淆。总之,它应该是这样的:
@interface SecondViewController : UIViewController{
NSString *text;
}
@property (retain, nonatomic) NSString *text;
@end
将
text
声明为实例变量是可选的——如果您不这样做,编译器将创建一个ivar,如果您为text
属性合成访问器。您的想法是正确的,但是您试图在第二个指向有效对象之前调用-setText:
!改为这样做:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
另外,您为secondView
类提供的接口看起来既不正确又不完整--我不确定您想用-IB
部分做什么。如果您遵循通常的Objective-C命名约定,并以大写字符开始类名:SecondView
而不是SecondView
,这将对将来有所帮助。最后,我建议不要将视图控制器命名为“…视图”,因为这样很容易将视图控制器与UIView混淆。总之,它应该是这样的:
@interface SecondViewController : UIViewController{
NSString *text;
}
@property (retain, nonatomic) NSString *text;
@end
将text
声明为实例变量是可选的——如果您不这样做,那么如果您为text
属性合成访问器,编译器将创建一个ivar。将代码更改为:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
在原始版本中,在对第二个视图调用setText:
后,您正在初始化(即实例化)它。您需要初始化它,然后设置文本。将代码更改为:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
在原始版本中,在对第二个视图调用setText:
后,您正在初始化(即实例化)它。您需要对其进行初始化,然后设置文本。您需要在分配和初始化后设置文本
-(IBAction) testButton {
secondView *second = [[[secondView alloc] initWithNibName:nil bundle:nil] autorelease];
[second setText:text];
[self presentModalViewController:second animated:YES];
}
您需要在分配和初始化后设置文本
-(IBAction) testButton {
secondView *second = [[[secondView alloc] initWithNibName:nil bundle:nil] autorelease];
[second setText:text];
[self presentModalViewController:second animated:YES];
}