传递一个对象:多个uiviewcontroller

时间:2013-05-23 19:48:27

标签: ios objective-c uiviewcontroller

这是我的应用程序的参数:我有3个viewcontrollers,viewcontroller1viewcontroller2viewcontroller3。 在viewcontroller1中,我只有一个按钮,点击此按钮我添加viewcontroller2,作为弹出窗口,其中包含一个文本字段。再次编辑我的文本字段时,我添加viewcontroller3只包含一个选择器视图。我要做的是从选择器视图传递选定的对象,并在我完成按钮时将其传递到viewcontroller2的文本字段。

这是我的代码:

//Viewcontroller1 : add the Viewcontroller2 
- (void)withContractAction:(id)sender
{
    Viewcontroller2 *addViewController = [[Viewcontroller2 alloc] initWithNibName:@" Viewcontroller2 " bundle:nil];

    addViewController.delegate = self;

    addViewController.modalPresentationStyle = UIModalPresentationFormSheet;
    addViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentModalViewController:addViewController animated:YES];

    addViewController.view.superview.frame = CGRectMake(150, 300, 460, 300);

    [addViewController release];
}
viewcontroller2

我添加了viewcontroller3

-(void)textFieldDidBeginEditing:(UITextField *)textField{


        [textField resignFirstResponder];
        Viewcontroller3 *addViewController = [[Viewcontroller3 alloc] initWithNibName:@" Viewcontroller1" bundle:nil];
        addViewController.delegate = self;

        addViewController.modalPresentationStyle = UIModalPresentationFormSheet;
        addViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

        [self presentModalViewController:addViewController animated:YES];

        addViewController.view.superview.frame = CGRectMake(50, 740, 695, 245);
               self.textfield.text = contrat.NUM_CONTRACT;
    }
}

viewcontroller3:

-(IBAction)done:(id)sender{
    Viewcontroller2 *popup = [[Viewcontroller2 alloc]init];
//contrat is my object that I need to pass , from  Viewcontroller3 to Viewcontroller2 ,so I included it into my interface in Viewcontroller2 , 
        popup.contrat = sometxt;
    [self.navigationController pushViewController:popup animated:YES];

    [self dismissModalViewControllerAnimated:YES];
    [orderForm dismissModalViewControllerAnimated:YES];

    [popup release];   
}

1 个答案:

答案 0 :(得分:0)

不要这样做:Viewcontroller2 *popup = [[Viewcontroller2 alloc]init];

相反,请使用ViewController3的{​​{1}},它应该是delegate的现有实例,而不是您即时发明的新实例。