使用协议传回数据

时间:2014-10-15 20:44:30

标签: ios objective-c

我需要将一些数据传递给以前的视图控制器,我的代码有什么问题? 在这段代码" contactViewController"是我的第一个视图控制器" groupViewController"是我的第二个视图控制器

//ContactEditVC.h (firstViewController)

#import "GroupEditTVC.h"

@interface ContactEditVC : UIViewController <SecondViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIImageView * imageView;
    UIButton * choosePhotoBtn;
    UIButton * takePhotoBtn;
    UIButton * btnGroup;
}

@property (nonatomic, strong) NSString *groupName;


---------------------------------------------------
//ContactEditVC.m (firstViewController)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"selectGroup"]){
        //get selected contact


        //pass selected contact to MyContactAppViewController for editing
        GroupEditTVC *destViewcontroller=segue.destinationViewController;
        destViewcontroller.delegate=self;

    }
}


-(void)viewWillAppear:(BOOL)animated
{
    self.txtFname.text=groupName;

}

- (void)dataFromController:(NSString *)data
{
   groupName=data;
}

在我的第二个视觉控制器中:

@protocol SecondViewControllerDelegate <NSObject>
- (void)dataFromController:(NSString *)data;
@end

@interface GroupEditTVC : UITableViewController <UIAlertViewDelegate>

@property (retain) id <SecondViewControllerDelegate> delegate;

@end
---------------------------------
//GroupEditTVC.m (secendViewController)


#import "ContactEditVC.h"

@interface GroupEditTVC ()
@end

@synthesize delegate;



- (IBAction)donePressed:(id)sender {  
    [[self delegate]dataFromController:@"blabla"];
    [self dismissViewControllerAnimated:YES completion:nil];    
}

1 个答案:

答案 0 :(得分:0)

在 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender中设置一些断点。我猜测[segue identifier]不等于你认为它应该是什么,因此它永远不会进入if语句中的代码块。