2012-08-10 67 views
1

我有2个ViewControllers,在1st - TableView和2nd - 按钮上有标签。当我点击第二个ViewController中的按钮时,我需要返回到TableView并设置在iOS中的授权

cell.detailTextLabel.text 

来自按钮上标签的文本。

这里是我的代码,但它不工作:

ViewController.h:

#import <UIKit/UIKit.h> 
#import "TestControl.h" 

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, myTestProtocol> 
{ 
    TestControl *myProtocol; 
} 
@property (strong, nonatomic) IBOutlet UITableView * tableTest; 

@end 

ViewController.m:

#import "ViewController.h" 
#import "TestControl.h" 

@implementation ViewController 

@synthesize tableTest; 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.navigationController.navigationBarHidden = YES; 

    myProtocol = [[TestControl alloc]init]; 
    myProtocol.delegate = self; 

     // Do any additional setup after loading the view, typically from a nib. 
} 

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 3; 
} 

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    TestControl * test = [[TestControl alloc] initWithNibName:@"TestControl" bundle:nil]; 
    [self.navigationController pushViewController:test animated:YES]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text = @"Firs Cell"; 
      cell.detailTextLabel.text = myProtocol.myLabel.text; 
      break; 
     case 1: 
      cell.textLabel.text = @"Second Cell"; 
      break; 
     case 2: 
      cell.textLabel.text = @"Third Cell"; 
      break; 


     default: 
      break; 
    } 

    return cell; 
} 
@end 

TestControl.h:

#import <UIKit/UIKit.h> 

@protocol myTestProtocol <NSObject> 

@end 

@interface TestControl : UIViewController 
{ 
    UILabel *myLabel; 
} 

@property (nonatomic, assign) id <myTestProtocol> delegate; 
@property (strong, nonatomic) IBOutlet UILabel *myLabel; 

- (IBAction)testButton:(id)sender; 
@end 

TestControl.m:

@implementation TestControl 

@synthesize myLabel; 
@synthesize delegate; 


- (IBAction)testButton:(id)sender 
{ 
    myLabel.text = @"TEXT LABEL"; 
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES]; 

} 

这是什么问题?

+0

您应该使TestControl.h中的代理“弱”而不是“分配”。看看这个SO帖子:http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – 5StringRyan 2012-08-10 23:38:51

+0

我建议你从这个[教程]开始(http:// www .raywenderlich.com/5191 /开始 - 故事板 - 在 - ios-5-part-2)关于使用委派。 – Rick 2012-08-11 03:55:23

回答

1

几件事情....

您将创建两个不同的TestControl对象,设置了委托他们中的一个,推动另外一个,所以一个操作按钮水龙头没有委托。

委托逻辑会更好地反过来。也就是说,TestControl应该具有与其代表通信的代码,而不是代表从TestControl“拉”的代码。