Ios 如何将数据从UIContainerView传递到同一父视图控制器中的其他UIContainerView?
我正在努力将数据从一个Ios 如何将数据从UIContainerView传递到同一父视图控制器中的其他UIContainerView?,ios,iphone,objective-c,xcode,uicontainerview,Ios,Iphone,Objective C,Xcode,Uicontainerview,我正在努力将数据从一个UIContainerView获取到另一个UIContainerView。例如:我有两个容器,分别是calculatorContainer和displaytresultcontainer。因此,当我按下“=”按钮计算结果时,我希望它显示在显示结果容器中。我已经尝试了segue方法和parentViewController访问的不同选项,但仍然没有成功。这里有一个简单的方法,但首先你应该知道这个方法适用于正向流,而不是反向流(在反向流中,你需要一个称为委托的东西): 在vie
UIContainerView
获取到另一个UIContainerView
。例如:我有两个容器,分别是calculatorContainer
和displaytresultcontainer
。因此,当我按下“=”按钮计算结果时,我希望它显示在显示结果容器中。我已经尝试了segue方法和parentViewController访问的不同选项,但仍然没有成功。这里有一个简单的方法,但首先你应该知道这个方法适用于正向流,而不是反向流(在反向流中,你需要一个称为委托的东西):
在view2中,在其.h
文件中获取文本字段和NSString
:
@property(nonatomic,strong) NSString *myResult; //to contain the result
@property(nonatomic,strong) IBOutlet UITextfield *myResultText; // to display the result
在第一个视图(视图1)中,获取输入并添加(添加+)等按钮:
将该插座(按钮)与视图控制器连接。然后为该按钮添加方法并将其连接到该按钮:
-(int)addFunc;
然后从您已经在第一个视图中创建的两个文本字段中获取用户输入(input1TextField
和input2TextField
):
在addFunc
中写入:
(int)addFunc{
int input1,input2,result;
input1 = self.input1TextField.text;
input2 = self.input2TextField.text;
result = input1+input2;
return result;
}
现在在iAction
方法中写入:
-(IBAction)myIbaction{
[self performSegueWithIdentifier @"seguename", sender :self]; // set the segue name and fill it here.
SecondViewController * sec = [segue destinationViewController];
int result = [self addFunc];
sec.myResult = (NSString)result;
}
-(void)viewDidLoad{
self.myResultText = self.myResult;
}
现在在viewDidLoad
方法中的第二个视图(view2)中写入:
-(IBAction)myIbaction{
[self performSegueWithIdentifier @"seguename", sender :self]; // set the segue name and fill it here.
SecondViewController * sec = [segue destinationViewController];
int result = [self addFunc];
sec.myResult = (NSString)result;
}
-(void)viewDidLoad{
self.myResultText = self.myResult;
}
它将显示您的结果。
不知道这是否有帮助,但你肯定会明白,这就是你将如何执行它。
希望有帮助。您必须在第二个UIContainerView
中声明此方法:
UIContainerView2
-(id)initWithsetresult:(Int)Result {
int showPreResult = result;
return self;
}
按按钮操作:
antagonist = [[UIContainerView2 alloc]initWithsetresult: calculateResult];
使用代理
步骤:
从第一个UIContainerView调用父视图控制器的委托方法
然后,parentview控制器将该值传递给第二个UIContainerView
有两种可能性。
1.使用appDelegate。使用应用程序委托中的属性在容器之间传递数据。
把这个放在第一个容器里
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDelegate.dataToPass=dataToPass;
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
dataToPass=appDelegate.dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
parent.dataToPass=dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
data=parent.dataToPass;
在第二个容器中
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDelegate.dataToPass=dataToPass;
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
dataToPass=appDelegate.dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
parent.dataToPass=dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
data=parent.dataToPass;
2.使用ParentViewController
在第一个容器中
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDelegate.dataToPass=dataToPass;
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
dataToPass=appDelegate.dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
parent.dataToPass=dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
data=parent.dataToPass;
在第二个容器中
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDelegate.dataToPass=dataToPass;
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
dataToPass=appDelegate.dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
parent.dataToPass=dataToPass;
ParentViewController parent=(ParentViewController *)[self parentViewController];
data=parent.dataToPass;
请记住在您编写的代码中添加缩进,以便正确显示。(:抱歉,我是stackoverflow新手,如果有任何错误,请原谅我。没问题!您会在使用时习惯这些指导原则和格式规则。请确保您的帖子符合这些指导原则。(:AppDelegate方法对我很有效,非常简单。我让自己太难了!谢谢您的帮助!