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