1
votes

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.

I need this string to be have some value

@property (nonatomic,strong) NSString *str;

When the following method is getting called.

[view getTotal];

Label is in the first view controller.

Thanks in advance.

In 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

#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
first clear your question with which controller you got null value ? and when you need value and which type ? - Himanshu Moradiya
Ok check the edit. Thanks - user7755257
- (IBAction)backBtn:(id)sender { ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"]; VC. str = String(num); [self presentViewController:VC animated:YES completion:nil]; } - Himanshu Moradiya
@HimanshuMoradiya Sir, i want to assign the value to string when i have called method getTotal; is it possible to do that. If it is please suggest some code. I can also do that which code u have suggested me. - user7755257
i suggest you to set a value in NSUserDefault and just retrive value from it your problem solve you did not call any other method i think that. becz first time in your controller you got null value when you move controller then you set a value from second controller and when you return back its value nil so i suggest you just set value in NSuserdefault - Himanshu Moradiya

1 Answers

0
votes

What I can understand from your code is that you are presenting secondViewController from ViewController on a button click. On Second view controller you want to add two numbers and show them on Firstview controller again.

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.