只需使用delegate
。在推送'UploadViewController'實例之前,您需要將其設置爲delegate
,並將其設置爲self
(位於GoogleDocMainPageController.m中)。
[self.delegate setDataAfterSelectedTabelCell:[NSString stringWithFormat:@"TalbeCell %d selected", [indexPath row]]];
主代碼如下所示:每次的TABEL小區被選擇,它會爲self.delegate
通過分派self.delegate
的方法,該方法是通過GoogleDocMainPageController實現設定值(這裏是GoogleDocMainPageController實例)
UploadViewController.h:
#import <UIKit/UIKit.h>
@class UploadViewController;
@protocol UploadViewControllerDelegate <NSObject>
- (void)setDataAfterSelectedTabelCell:(NSString *)stringValueInCell;
@end
@interface UploadViewController : UITableViewController
@property (nonatomic, retain) id <UploadViewControllerDelegate> delegate;
@end
UploadViewController.m:
//...
@synthesize delegate = _delegate;
//...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate setDataAfterSelectedTabelCell:[NSString stringWithFormat:@"TalbeCell %d selected", [indexPath row]]];
}
GoogleDocMainPageController.h:
#import <UIKit/UIKit.h>
#import "UploadViewController.h"
@class UploadViewController;
@interface GoogleDocMainPageController : UIViewController <UploadViewControllerDelegate>
- (void)loadUploadViewController;
@property (nonatomic, retain) UILabel * glLabel;
@property (nonatomic, retain) UploadViewController * uploadViewController;
@end
GoogleDocMainPageController.m:
//...
@synthesize glLabel = _glLabel;
@synthesize uploadViewController = _uploadViewController;
//...
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton * uploadButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 160.0f, 300.0f, 35.0f)];
[uploadButton setBackgroundColor:[UIColor blackColor]];
[uploadButton setTitle:@"Upload Button" forState:UIControlStateNormal];
[uploadButton addTarget:self action:@selector(loadUploadViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:uploadButton];
[uploadButton release];
self.glLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 200.0f, 300.0f, 35.0f)];
[self.glLabel setBackgroundColor:[UIColor blackColor]];
[self.glLabel setTextColor:[UIColor whiteColor]];
[self.glLabel setTextAlignment:UITextAlignmentCenter];
[self.glLabel setText:@"Default"];
[self.view addSubview:self.glLabel];
self.uploadViewController = [[UploadViewController alloc] initWithStyle:UITableViewStylePlain];
}
//...
#pragma mark -
- (void)loadUploadViewController
{
[self.uploadViewController setDelegate:self];
[self.navigationController pushViewController:self.uploadViewController animated:YES];
}
#pragma mark - UploadViewControllerDelegate
- (void)setDataAfterSelectedTabelCell:(NSString *)stringValueInCell
{
[self.glLabel setText:stringValueInCell];
}
代表和協議的簡單情況...... http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html –
搜索關於委託變量... – Maulik