我正在努力将我的数据从 a 获取UIContainerView
到另一个UIContainerView
。例如:我有 2 个容器,它们是calculatorContainer
和displayResultContainer
。因此,当我按“=”按钮计算结果时,我希望它显示在displayResultContainer
. 我已经尝试过使用 segue 方法和parentViewController
访问的不同选项,但仍然没有运气。
4 回答
有两种可能性。1.使用appDelegate。使用应用程序委托中的属性在容器之间传递数据。把它放在第一个容器中
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDelegate.dataToPass=dataToPass;
在第二个容器中
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
dataToPass=appDelegate.dataToPass;
2.使用ParentViewController。
在第一个容器中
ParentViewController parent=(ParentViewController *)[self parentViewController];
parent.dataToPass=dataToPass;
在第二个容器中
ParentViewController parent=(ParentViewController *)[self parentViewController];
data=parent.dataToPass;
使用代表。
脚步:
从第一个 UIContainerView,调用父视图控制器的委托方法。
然后父视图控制器将值传递给第二个 UIContainerView。
您必须在第二个中声明此方法UIContainerView
:
UIContainerView2
-(id)initWithsetresult:(Int)Result {
int showPreResult = result;
return self;
}
按钮操作:
antagonist = [[UIContainerView2 alloc]initWithsetresult: calculateResult];
这是一个简单的方法,但首先你应该知道这个方法适用于正向流而不是反向流(在反向流中你需要一个叫做委托的东西):
View 1 = calculator
View 2 = result display.
在 view2 中获取一个文本字段和一个NSString
在其.h
文件中的:
@property(nonatomic,strong) NSString *myResult; //to contain the result
@property(nonatomic,strong) IBOutlet UITextfield *myResultText; // to display the result
在第一个视图 (view1) 中接受输入并添加按钮,例如 (addition +) 等:
@property(nonatomic,strong) IBOutlet UIButton *addition;
将该插座(按钮)与视图控制器连接。然后为该按钮添加一个方法并将其连接到该按钮:
-(int)addFunc;
然后从您已经在第一个视图(input1TextField
和input2TextField
)中创建的 2 个文本字段中获取用户输入:
里面addFunc
写:
(int)addFunc{
int input1,input2,result;
input1 = self.input1TextField.text;
input2 = self.input2TextField.text;
result = input1+input2;
return result;
}
现在在IBaction
方法里面写:
-(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;
}
现在在方法中的第二个视图(view2)中viewDidLoad
写入:
-(void)viewDidLoad{
self.myResultText = self.myResult;
}
它会显示你的结果。不知道它是否有帮助,但你肯定会明白这是你能够执行它的方式。希望能帮助到你。