I'm just a beginner in iOS Devp and stuck with this problem since last 2days. What I am trying to do is I've two view controllers, the first View Controller consists of 2 buttons and I want to load a specific type of data in next View controller on respective button action.
What I did is as follows : I've two ViewControllers connected using segue with id "showDetailSegue", 1. ViewController 2. SecondVC I want to update label on SecondVC when the button on ViewController is tapped.
//ViewController.h
#import <UIKit/UIKit.h>
#import "SecondVC.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
//ViewController.m
#import "ViewController.h"
#import "SecondVC.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
NSString *str = @"my string data..";
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showDetailSegue"]){
SecondVC *controller = segue.destinationViewController;
controller.getString = str;
controller.testLabel.text = str;
}
}
@end
//SecondVC.h
#import <UIKit/UIKit.h>
@interface SecondVC : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@property (nonatomic, strong) NSString *getString;
@end
//SecondVC.m
Please help me with a straightforward and clear explanation.
Thanks in advance.!
There are many ways to pass data to desired ViewController
.
1. Using segue
Let suppose you have to pass a string to another VC.
#import "ViewController.h"
#import "SecondVC.h"
@interface ViewController ()
// all instance global variable should be declare here
NSString str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
str = @"my string data..";
}
// segue delegate method
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"segueIdentifireName"]) {
SecondVC *destViewController = segue.destinationViewController;
destViewController.getString = str;
}
}
@end
Now you must have to create NSString
object inside destinationVc .h
file like below.
@interface SecondVC : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@property (nonatomic, strong) NSString *getString;
@end
Inside .m
file get string data like:
@interface ViewController ()
@end
@implementation SecondVC
- (void)viewDidLoad {
[super viewDidLoad];
self.testLabel.text = getString; // we passed `str` data inside `getString` object so it can be refelect here using `getString` variable.
NSLog(@"string data %@", getString);
}
2. Using storyboard id:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YourViewControllerClassName *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewContIdentifire"];
vc.getString = str;
[self.navigationController pushViewController:vc animated:YES];
Inside .h
file:
@property (nonatomic, strong) NSString *getString;
Inside .m
file get string data like:
NSLog(@"string data %@", getString);
In Swift3
let controller = UIStoryboard().getControllerInstance(storyBoardName: "MainStoryboard", identifire: "viewContIdentifire")
controller.getString = str;
self.navigationController?.pushViewController(controller, animated: true)
To get data back:
Create protocol where you need to send data back.
// dec var on top
var delegate: YourDelegate!
protocol YourDelegate {
func delegateFunction(value: String)
}
Call delegate func on tap action:
delegate.delegateFunction(value: "My sample string")
Receiving controller
Implement the YourDelegate on top.
func delegateFunction(value: String){ print("Got: ", value) }
3. Using NSUserDefaluts
( Not recommended ).
4. Using local DB.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.