2012-08-10 71 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「拉」的代碼。