1

我很难在 UISplitViewController 内的两个视图控制器之间进行数据通信。我正在关注教程。我能够在主视图和详细视图上创建一个带有 UITableViews 的拆分视图控制器。现在,我真正想要的是,当我点击主表中的特定行时,它必须向详细视图发送一些值。

我只是在玩一个自定义委托,将一些值从一个视图控制器传递到另一个视图控制器,以查看它们之间是否有任何通信,但似乎没有任何效果。

在 MasterTableView.h

@protocol sendingProtocol <NSObject>

-(void)passSomeValue:(NSString *)someValue;

@end



@interface MasterTableView : UITableViewController
{
    NSArray *menuArray;
    id<sendingProtocol>delegate;
}

@property (nonatomic,assign) id<sendingProtocol>mydelegate;

@end

在 .m 文件中合成。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[self mydelegate] passSomeValue:@"Some Value"];
}

在 DetailTableView.h

-(void)passSomeValue:(NSString *)someValue
{
    NSLog(@"%@", someValue);
}

请注意,我在 ViewDidLoad 方法中调用 mydelegate。这是写法吗?有人可以帮忙吗?

- (void)viewDidLoad
{
    [super viewDidLoad];
    MasterTableView *masterView = [[MasterTableView alloc] init];
    masterView.mydelegate = self;
}

Thank you in advance!

4

1 回答 1

1

In viewDidLoad method of your DetailTableView you should not create a new MasterTableView object. The error is here in this method:

- (void)viewDidLoad
{
    [super viewDidLoad];
    MasterTableView *masterView = [[MasterTableView alloc] init];
    masterView.mydelegate = self;
}

You are creating another object of MasterTableView and setting its delegate to self and hence all the problem.

To set the delegate of MasterTableView to DetailTableView, go to AppDelegate.h. You must have defined the MasterTableView and DetailTableView objetcs in AppDelegate.

 //Set the DetailTableView as the master's delegate.
self.masterTableView.delegate = self.detailTabelView;
于 2013-09-16T14:23:14.060 回答