我是 iphone 开发和 Xcode 的新手。而且我很难运行一个简单的演示应用程序,以编程方式将我的文本字段文本从一个视图传递到另一个视图中的标签....而且我不知道我哪里出错了[甚至 NSLOG 来检查字符串的到来。 ..它说空:(]
看看你们能不能帮我找到我缺少代码的地方?提前致谢!
这是我做的事情:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>{
NSString *str;
}
@property(nonatomic,retain)NSString *str;
@end
视图控制器.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];
}
下一个视图控制器.h *
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface nextViewController : UIViewController{
NSString *str;
}
@property(nonatomic,retain)NSString *str;
@end
下一个视图控制器.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];
}