为啥字符串值为空?

Posted

技术标签:

【中文标题】为啥字符串值为空?【英文标题】:Why the String value is null?为什么字符串值为空? 【发布时间】:2017-03-23 06:58:20 【问题描述】:

为什么字符串值得到空值。有人请找出错误吗? 当从 SecondViewController 调用该方法时,我如何获取字符串值我需要另一个字符串还是有人请提出一些更正建议。

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

@property (nonatomic,strong) NSString *str;

当下面的方法被调用时。

[查看getTotal];

标签在第一个视图控制器中。

提前致谢。

在 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

【问题讨论】:

首先清除您的问题,您使用哪个控制器获得了空值?以及何时需要价值以及哪种类型? 好的,检查编辑。谢谢 - (IBAction)backBtn:(id)sender ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];风险投资。 str = 字符串(数字); [自我presentViewController:VC动画:YES完成:无]; @HimanshuMoradiya 先生,我想在调用 getTotal 方法时将值分配给字符串;有可能这样做吗?如果是,请建议一些代码。我也可以执行您建议的代码。 我建议您在 NSUserDefault 中设置一个值并从中检索值您的问题解决了您没有调用任何其他我认为的方法。因为第一次在您的控制器中,当您移动控制器时您得到空值,然后您从第二个控制器设置一个值,当您返回它的值 nil 所以我建议您只在 NSuserdefault 中设置值 【参考方案1】:

我从您的代码中可以理解的是,您在单击按钮时从 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。

【讨论】:

我没有用导航控制器推送视图控制器,所以我们在数组中得到的值是 nil。我很感激你尝试过。谢谢

以上是关于为啥字符串值为空?的主要内容,如果未能解决你的问题,请参考以下文章

为啥在 MVC 项目中返回的表单值为空?

gson实体转json时当字段值为空时,json串中就不存在该属于,请问如何在值为空的时候也保留该字符串

Ajax:为啥response.responseXML返回的值为空?

为啥字符串可以为空? [复制]

去除数组里面值为空或者为空字符串的元素

XStream将java对象解析为xml字符串时,过滤掉节点值为空的节点