为什么String值为null?

时间:2017-03-23 06:58:21

标签: ios objective-c iphone

为什么字符串值变为空值。有人请找错误? 如何从SecondViewController调用方法时获取字符串值我需要另一个字符串或有人请建议一些更正。

我需要这个字符串有一些值

@property(非原子,强)NSString * str;

调用以下方法时。

[查看getTotal];

Label位于第一个视图控制器中。

提前致谢。

在ViewController.m中

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
{
    NSInteger total;

}
@property (weak, nonatomic) IBOutlet UILabel *lbl;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (nonatomic,strong) NSString *str;

- (id)initWithInitialNumber:(NSInteger)initialNumber;

- (void)addNumber:(NSInteger)newNumber;

- (NSInteger)getTotal;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",_str);
    self.lbl.text = _str;
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear called");
    [self viewDidLoad];
}
-(id)initWithInitialNumber:(NSInteger)initialNumber{

    total = initialNumber;
    return self;

}

-(void)addNumber:(NSInteger)newNumber{

    total += newNumber;

}
-(NSInteger)getTotal{

    NSLog(@"Number is: %ld",total);

    self.str = [NSString stringWithFormat:@"%ld",total]; 
    NSLog(@"%@",self.str);
    return total;
}

- (IBAction)btnAct:(id)sender {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SecondViewController *SC = [sb instantiateViewControllerWithIdentifier:@"SC"];
    [self presentViewController:SC animated:YES completion:nil];


}

-(NSString *)str{
    return _str;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

在SecondViewController.m中

#import "SecondViewController.h"
#import "ViewController.h"

@interface SecondViewController ()<Sendata>
{
    NSInteger *num;
}

@end

@implementation SecondViewController
- (IBAction)backBtn:(id)sender {

    ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];

    [self presentViewController:VC animated:YES completion:nil];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    ViewController *view = [[ViewController alloc]initWithInitialNumber:10];
    view.delegate = self;
    [self Print:@"Hello"];
    [view addNumber:2];
    [view getTotal];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)Print:(NSString *)str{

    NSLog(@"%@",str);

}
@end

1 个答案:

答案 0 :(得分:0)

我可以从您的代码中了解到,您只需单击按钮即可从ViewController呈现secondViewController。在第二个视图控制器上,您想要添加两个数字并再次在Firstview控制器上显示它们。

what I can see from your code , you are pushing the new instance of ViewController every time. So thevalue you are intialising in your view didLoad in diffrent object then the one your pushing again on button action of back. IF you can modify you code like this , it will work : 


@interface ViewController ()
{
    NSInteger total;

}
@property (weak, nonatomic) IBOutlet UILabel *lbl;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (nonatomic,strong) NSString *str;

- (id)initWithInitialNumber:(NSInteger)initialNumber;

- (void)addNumber:(NSInteger)newNumber;

- (NSInteger)getTotal;

@end
@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",_str);
    self.lbl.text = _str;
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear called");
    [self viewDidLoad];
}
-(id)initWithInitialNumber:(NSInteger)initialNumber{

    total = initialNumber;
    return self;

}

-(void)addNumber:(NSInteger)newNumber{

    total += newNumber;

}
-(NSInteger)getTotal{

    NSLog(@"Number is: %ld",total);

    self.str = [NSString stringWithFormat:@"%ld",total];
    NSLog(@"%@",self.str);
    return total;
}

- (IBAction)btnAct:(id)sender {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SecondViewController *SC = [sb instantiateViewControllerWithIdentifier:@"SC"];
    [self.navigationController pushViewController:SC animated:YES];

}

-(NSString *)str{
    return _str;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


@interface SecondViewController ()
{
    NSInteger *num;
    ViewController *viewController;
}

@end

@implementation SecondViewController
- (IBAction)backBtn:(id)sender {

    [self.navigationController popViewControllerAnimated:YES]


}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *VCs = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
    if ([[VCs objectAtIndex:[VCs count] - 2] isKindOfClass:[ViewController class]])
    {
        ViewController *view = [[VCs objectAtIndex:[VCs count] - 2]
                                view.delegate = self;
                                [self Print:@"Hello"];
                                [view addNumber:2];
                                [view getTotal];
    }

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)Print:(NSString *)str{

    NSLog(@"%@",str);

}
@end

我已经将ViewController的实例存在于导航堆栈中,更改了值,并且在后面我已经弹出了SecondViewController。

相关问题