找到了....委托是解决方案...只是希望它是最有效的!这是委托的代码。
首先,实现 parentViewcontroller 的委托及其方法,还要确保将委托添加到 parentviewcontroller...
@protocol SendFeedBackDelegate
- (void) didReceiveType:(NSString *) message;
@end
@interface SendFeedBackViewController : UIViewController <SKPSMTPMessageDelegate, UITableViewDataSource,UITableViewDelegate, SendFeedBackDelegate>
{
NSString *subject;
}
接下来,实现方法: - (void) didReceiveType:(NSString *) message;
下
@implementation SendFeedBackViewController
- (void) didReceiveType:(NSString *) message
{
subject = message;
[feedbackTableView reloadData];
// I reload the data because it is needed when this function is going to be called
// in the child viewcontroller.... just keep reading :)
}
现在去- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
使用这个例子和我的项目:)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [feedbackTableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// THIS IS THE IMPORTANT PIECE OF CODE YOU NEED TO NOTICE.....
// it allows for the first thing the tableview cell to be is a static string until subject
// it is changed and the user chooses a subject in the childviewcontroller
if (subject == nil) {
cell.textLabel.text = @"Select a Product";
} else {
cell.textLabel.text = subject;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
现在,给 childviewcontroller 添加一个协议,让 childviewcontroller 符合 parentviewcontroller
在 childviewcontroller.h 中:添加这几行代码,
#import "ParentViewController.h"
@protocol SendFeedBackDelegate;
@interface FeedbackTypes : UITableViewController
{
id<SendFeedBackDelegate> delegate;
}
@property (nonatomic, assign) id<SendFeedBackDelegate> delegate;
现在您已经在父视图控制器中设置了委托....接下来在相同的文件实现文件(.m)上添加这些:
//Add synthesize just under @implementation "ClassName"
@synthesize delegate;
// I used a uitableviewcontroller for this example so refer to the problem I have above
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSLog(@"Cell's text: %@",cell.textLabel.text);
[delegate didReceiveType:cell.textLabel.text];
[self.navigationController popViewControllerAnimated:YES];
}
就是这样!!!!..... :),希望这是一个简单而基本的教程,这是一个快照。