0

我正在努力将我的数据从 a 获取UIContainerView到另一个UIContainerView。例如:我有 2 个容器,它们是calculatorContainerdisplayResultContainer。因此,当我按“=”按钮计算结果时,我希望它显示在displayResultContainer. 我已经尝试过使用 segue 方法和parentViewController访问的不同选项,但仍然没有运气。

4

4 回答 4

1

有两种可能性。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;
于 2014-07-07T09:43:34.033 回答
1

使用代表。

脚步:

  1. 从第一个 UIContainerView,调用父视图控制器的委托方法。

  2. 然后父视图控制器将值传递给第二个 UIContainerView。

于 2014-07-07T09:15:42.797 回答
0

您必须在第二个中声明此方法UIContainerView

UIContainerView2
-(id)initWithsetresult:(Int)Result {
   int showPreResult = result;
   return self;
}

按钮操作:

 antagonist = [[UIContainerView2 alloc]initWithsetresult: calculateResult];
于 2014-07-07T09:09:07.163 回答
0

这是一个简单的方法,但首先你应该知道这个方法适用于正向流而不是反向流(在反向流中你需要一个叫做委托的东西):

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;

然后从您已经在第一个视图(input1TextFieldinput2TextField)中创建的 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;
}

它会显示你的结果。不知道它是否有帮助,但你肯定会明白这是你能够执行它的方式。希望能帮助到你。

于 2014-07-07T09:07:24.840 回答