[英]How to pas data between view controller without using segue in ios?
Helo all,大家好,
I am moving from one view controller to another using below code.我正在使用下面的代码从一个视图控制器移动到另一个视图控制器。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Now i want to pass some data from one view controller to another like some variable.I have got this code to do this task but i am not making instance of another view controller.I am starting another view controller using storyboard.现在我想将一些数据从一个视图控制器传递到另一个视图控制器,比如某个变量。我有这个代码来完成这个任务,但我没有制作另一个视图控制器的实例。我正在使用故事板启动另一个视图控制器。
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];
Please tell me how can i pass data between view controllers in my way?请告诉我如何以我的方式在视图控制器之间传递数据?
Solution does not work decalre NSString
in .h
file.解决方案在
.h
文件中不起作用decalre NSString
。
@property (nonatomic, strong) NSString *UserId;
Synthesize in .m file在 .m 文件中合成
@synthesize UserId;
Code for navigation导航代码
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
vc.UserId=@"abc";
[self.navigationController pushViewController:vc animated:YES];
But vc.UserId
does not recognized.但
vc.UserId
无法识别。
You can pass data between two ViewControler with the help of properties.您可以借助属性在两个 ViewControler 之间传递数据。 First in RoloBusinessCard.h ViewControler make a property like this
首先在 RoloBusinessCard.h ViewControler 中创建这样的属性
@property (nonatomic, strong) NSString *str1;
in .m file of this class synthesize it like this在这个类的 .m 文件中像这样合成它
@synthesize str2;
Now like this you can pass value现在像这样你可以传递值
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
vc.str2=str2
[self.navigationController pushViewController:vc animated:YES];
If you are not able to get str2 in RoloBusinessCard如果您无法在 RoloBusinessCard 中获得 str2
First Replace UIViewController with Your Viewcontroller Name like this首先像这样用你的视图控制器名称替换 UIViewController
RoloBusinessCard *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
or或者
typecase UIViewController like this像这样打字的 UIViewController
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
(RoloBusinessCard *)vc1=vc
vc1.str2=str2
[self.navigationController pushViewController:vc1 animated:YES];
Hope it help.希望有帮助。
Try Like this, Hope it will work.像这样尝试,希望它会起作用。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
vc.str1 = str;
[self.navigationController pushViewController:vc animated:YES];
Here we are just using Navigation stack for passing data to the next controller这里我们只是使用导航堆栈将数据传递给下一个控制器
create a property in 2nd view controller在第二个视图控制器中创建一个属性
@property (nonatomic, strong) NSString *yourString;
in firstView controller action method object of view controller which you will create on push method do在你将在 push 方法上创建的视图控制器的 firstView 控制器动作方法对象中做
-(void)btnAtion{
object.yourstring = txtyourfirstvcObject.text
}
In swift, we can do the same在 swift 中,我们可以做同样的事情
let's suppose we want to pass a Bool Value to the next controller so we can do :假设我们想将 Bool 值传递给下一个控制器,以便我们可以这样做:
Class ToViewController : UIViewController {
public var isSelected : Bool?
}
Class FromViewController : UIViewController {
@IBAction func onClickFlyingFrom(_ sender: UIButton) {
let tvc = self.storyboard?.instantiateViewController(withIdentifier: "ToViewController") as! ToViewController
tvc.isSelected = true
self.navigationController?.present(vc, animated: true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.