我是iphone开发和Xcode的新手。 而且我很难运行一个简单的演示应用程序,用于将我的文本域文本从一个视图传递到另一个视图中的标签编程.... 而且我不知道我哪里出错了[甚至NSLOG检查字符串来了......它说Null :(]
看看你们是否可以帮我找到我缺少代码的地方? 提前谢谢!
这是我做的事情:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>{
NSString *str;
}
@property(nonatomic,retain)NSString *str;
@end
ViewController.m *
#import "ViewController.h"
#import "nextViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize str;
- (void)viewDidLoad
{
UITextField *txt =[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 200, 30)];
txt.borderStyle = UITextBorderStyleRoundedRect ;
txt.delegate=self;
[self.view addSubview:txt];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(80, 80, 200, 30);
[btn setTitle:@"Press" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[super viewDidLoad];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
self.str = [textField text];
return YES;
}
-(IBAction)btn:(id)sender{
nextViewController *next =[[nextViewController alloc]init];
[self.navigationController pushViewController:next animated:YES];
}
nextViewController.h *
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface nextViewController : UIViewController{
NSString *str;
}
@property(nonatomic,retain)NSString *str;
@end
nextViewController.m *
#import "nextViewController.h"
#import "ViewController.h"
@interface nextViewController ()
@end
@implementation nextViewController
@synthesize str;
- (void)viewDidLoad
{
UILabel *lbl = [[UILabel alloc]init];
lbl.frame=CGRectMake(40, 40, 200, 30);
ViewController *VC =[[ViewController alloc]init];
lbl.text=VC.str;`enter code here`
[self.view addSubview:lbl];
[super viewDidLoad];
}
答案 0 :(得分:0)
您没有将nextViewController
的{{1}}属性设置为您要将其设置为的字符串。
str
此外,您正在创建一个ViewController的新实例,没有明显的原因。只需使用以下命令将传递给nextViewController的字符串设置为标签:
-(IBAction)btn:(id)sender{
nextViewController *next =[[nextViewController alloc]init];
if (self.str)
next.str = self.str;
[self.navigationController pushViewController:next animated:YES];
}
答案 1 :(得分:0)
你的错误就在这里
- (void)viewDidLoad
{
UILabel *lbl = [[UILabel alloc]init];
lbl.frame=CGRectMake(40, 40, 200, 30);
ViewController *VC =[[ViewController alloc]init];
lbl.text=VC.str;`enter code here`
[self.view addSubview:lbl];
[super viewDidLoad];
}
您正在创建ViewController的新实例(因此str中没有有效值)。
你应该在这里将str从viewcontroller设置为nextviewcontroller:
-(IBAction)btn:(id)sender{
nextViewController *next =[[nextViewController alloc]init];
next.str = [textField text];
[self.navigationController pushViewController:next animated:YES];
}
并将viewdidlog从nextviewcontroller更改为
- (void)viewDidLoad
{
UILabel *lbl = [[UILabel alloc]init];
lbl.frame=CGRectMake(40, 40, 200, 30);
lbl.text=self.str;
[self.view addSubview:lbl];
[super viewDidLoad];
}
答案 2 :(得分:0)
尝试修改“btn”函数,如下所示:
-(IBAction)btn:(id)sender{
nextViewController *next =[[nextViewController alloc]init];
next.str = self.str; //<--- Add this.
[self.navigationController pushViewController:next animated:YES];
}
答案 3 :(得分:0)