-5

我是 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];
}
4

4 回答 4

0

我发现这个链接对你有帮助

http://pragmaticstudio.com/blog/2013/2/5/unwind-segues

于 2014-10-15T09:36:28.327 回答
0

您没有将nextViewController' 的属性设置str为您想要设置的字符串。

-(IBAction)btn:(id)sender{
    nextViewController *next =[[nextViewController alloc]init];
    if (self.str)
        next.str = self.str;
    [self.navigationController pushViewController:next animated:YES];
}

此外,您正在无缘无故地创建 ViewController 的新实例。只需使用以下内容将传递给 nextViewController 的字符串设置为标签:

lbl.text = self.str;
于 2013-05-08T16:06:18.940 回答
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];
}
于 2013-05-08T16:07:05.410 回答
0

尝试像这样修改“btn”函数:

-(IBAction)btn:(id)sender{
    nextViewController *next =[[nextViewController alloc]init];

    next.str = self.str; //<--- Add this.

    [self.navigationController pushViewController:next animated:YES];
}
于 2013-05-08T16:07:32.530 回答