简体   繁体   English

为什么String值为null?

[英]Why the String value is null?

Why the string value is getting null value. 为什么字符串值为空值。 Someone please find the error? 有人请找错误? How can i fetch the string value when the method is getting called from SecondViewController do i need a another string or someone please suggest some corrections. 如何从SecondViewController调用方法时获取字符串值我需要另一个字符串或有人请建议一些更正。

I need this string to be have some value 我需要这个字符串有一些价值

@property (nonatomic,strong) NSString *str; @property(nonatomic,strong)NSString * str;

When the following method is getting called. 当调用以下方法时。

[view getTotal]; [查看getTotal];

Label is in the first view controller. Label位于第一个视图控制器中。

Thanks in advance. 提前致谢。

In ViewController.m 在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

In SecondViewController.m 在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

What I can understand from your code is that you are presenting secondViewController from ViewController on a button click. 我从你的代码中可以理解的是你在按钮点击时从ViewController呈现secondViewController。 On Second view controller you want to add two numbers and show them on Firstview controller again. 在第二个视图控制器上,您要添加两个数字并再次在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

I have taken the instance of ViewController already present in navigation stack , changed the values and on back I have pop out the SecondViewController. 我已经将ViewController的实例已经存在于导航堆栈中,更改了值并且在后面我已经弹出了SecondViewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM