[英]How do I pass object from one ViewController to another ViewController
I have a RootViewController that calls an AddQuoteViewController and there is a variable "subject_id" that I set in the RootViewController that does not show up in AddQuoteViewController and I need help understanding why and how to fix it. 我有一个调用AddQuoteViewController的RootViewController,并且在RootViewController中设置了一个变量“ subject_id”,该变量未显示在AddQuoteViewController中,我需要帮助来了解为什么以及如何修复它。
Subject *sub = [self.tableDataSource objectAtIndex:indexPath.row];
self.subject_id = sub.subject_id;
[self addQuote_Clicked: sub];
... ...
- (void) addQuote_Clicked:(id)sender {
if(aqvController == nil)
aqvController = [[AddQuoteViewController alloc] initWithNibName:@"AddQuoteView" bundle:nil];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:aqvController];
[self.navigationController presentModalViewController:addNavigationController animated:YES];
}
Then in my AddQuoteViewController I try to access this variable like this: 然后在我的AddQuoteViewController中,尝试像这样访问此变量:
RootViewController *rv = [RootViewController alloc] ;
NSLog(@"rv.Subject_id = %d", rv.subject_id);
But get nothing. 但是什么也得不到。 There must be a simple way to do this.
必须有一种简单的方法来执行此操作。
First of all, there's an error in your third block of code. 首先,您的第三段代码中有一个错误。 This:
这个:
RootViewController *rv = [RootViewController alloc] ;
Should be this: 应该是这样的:
RootViewController *rv = [[RootViewController alloc]init];
But strictly speaking that's not why you aren't seeing your instance variable. 但是严格来说,这不是为什么您没有看到实例变量的原因。
If I understand correctly, the first two blocks of code are in RootViewController
, and they instantiate an AddQuoteViewController
and present it. 如果我理解正确,则代码的前两个块位于
RootViewController
,它们实例化一个AddQuoteViewController
并将其呈现。 Then, from your third block of code, which is in AddQuoteViewController
, you want to access a member variable ( subject_id
) of the RootViewController
that brought it up. 然后,在您的第三个代码块(位于
AddQuoteViewController
,您想要访问RootViewController
的成员变量( subject_id
)。
The approach of instantiating a RootViewController
from within the AddQuoteViewController
wouldn't work, because you're creating a different instance of RootViewController
. 从
AddQuoteViewController
内实例化RootViewController
的方法不起作用,因为您正在创建RootViewController
的另一个实例 。 What you're after is the value in the instance you just came from. 您所追求的是您刚来自的实例中的值。
Perhaps the easiest way to do it is to create a corresponding property on AddQuoteViewController
and set it when it's created: 也许最简单的方法是在
AddQuoteViewController
上创建相应的属性,并在创建时进行设置:
- (void) addQuote_Clicked:(id)sender {
if(aqvController == nil)
aqvController = [[AddQuoteViewController alloc] initWithNibName:@"AddQuoteView" bundle:nil];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:aqvController];
aqvController.subject_id = self.subject_id;
[self.navigationController presentModalViewController:addNavigationController animated:YES];
}
You'll need to create the subject_id
property on AddQuoteViewController
the same way you did on RootViewController
. 您需要像在
RootViewController
上一样在AddQuoteViewController
上创建subject_id
属性。
You can't access variables across view controllers like that. 您不能像这样跨视图控制器访问变量。 Have a look at creating a singleton class which will be globally accessible.
看一下如何创建可全局访问的单例类。 One exception I think is you can access them in the AppDelegate, but it's not advisable to have global vars.
我认为例外之一是您可以在AppDelegate中访问它们,但建议不要使用全局变量。
There are various ways of doing this, but a short answer - You can set a reference to the RootViewController as a property on your AddQuoteViewController 这样做的方法有很多种,但答案很简单-您可以将对RootViewController的引用设置为AddQuoteViewController上的一个属性。
ie 即
in AddQuoteViewController.h 在AddQuoteViewController.h中
RootViewController *rvc
... ...
@property (nonatomic,assign) RootViewController *rvc;
and corresponding synthesize and release in your implementation class. 并在实现类中进行相应的合成和发布。 (AddQuoteViewController.m)
(AddQuoteViewController.m)
Then when you create your AddQuoteViewController inside your RootViewController, also set this property: 然后,当您在RootViewController中创建AddQuoteViewController时,还要设置此属性:
- (void) addQuote_Clicked:(id)sender {
if(aqvController == nil)
aqvController = [[AddQuoteViewController alloc] initWithNibName:@"AddQuoteView" bundle:nil];
aqvController.rvc = self;
... etc.
Then you can access any property of the root view controller inside your AddQuoteViewController via this property: 然后,您可以通过以下属性访问AddQuoteViewController内部的根视图控制器的任何属性:
NSLog(@"rv.Subject_id = %d", self.rv.subject_id);
As a side note there are a few things you are doing in your question that are a bit unusual, like trying to get a reference to an object by allocating a new one, and also creating a new navigation controller and presenting it as a modal view controller typically you would do one or the other (and wouldn't need to create a new navigation controller). 附带说明一下,您在问题中所做的一些事情有些不寻常,例如尝试通过分配一个新对象来引用该对象,并创建一个新的导航控制器并将其显示为模式视图控制器通常您会做一个或另一个(并且不需要创建新的导航控制器)。 ie you would either create a view controller and present it modally, or you would create a view controller and push it onto your current navigation controller stack.
也就是说,您要么创建一个视图控制器并以模态形式显示它,要么创建一个视图控制器并将其推入当前的导航控制器堆栈中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.