3

编辑:

我有带有两个选项卡的 tabbarcontroller,第一个选项卡我有一个 viewController,第二个选项卡我有 navigationViewController 和两个带有 tableView 的 ViewControllers 堆栈。

Tab1--->VC1

Tab2--->NVC-->fistVC1----push to----->secondVC2。

我要推送的代码:

拳头VC1.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
       if (indexPath.section == 0 & (indexPath.row == 0)) {

    _secondVC2 = [[secondVC2 alloc] init];
    [self.navigationController pushViewController:_secondVC2 animated:YES];
    [_secondVC2 release];

what I need to do is set VC1 as a delegate of secondVC2, when secondVC2 cell selected i Want to send message to the VC1.

我该怎么做,请给我一些建议。

我试过了:

第二VC2.h

 @protocol secondVC2Delegate <NSObject>

   - (void)someMethod;

 @end
 #import <UIKit/UIKit.h>

  @interface secondVC2 :UIViewController<UITableViewDelegate,UITableViewDataSource>
 {
     id<secondVC2Delegate>delegate;
 }

@property (nonatomic ,assign) id<secondVC2Delegate>delegate;

 @end;

第二个VC2.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
     NSLog(@"delegate%@",self.delegate);
     if (indexPath.row == 0) {
      [self.delegate someMethod];
     }

VC1.h

    #import <UIKit/UIKit.h>
    #import"secondVC2"
    @interface VC1 :UIViewController<secondVC2Delegate>{

     secondVC2 *tvc2;

    }
    @property (nonatomic ,retain) secondVC2 *tvc2;

    - (void)someMethod;

    @end;

VC1.m

   - (void)viewDidLoad
    {
       tvc2 = [secondVC2 alloc] init];
       tvc2.delegate = self;
    }

但是代表一直被释放,我在 secondVC2 中得到了 nil 代表,我不知道为什么。那么我怎么能做到这一点呢?

4

1 回答 1

4
ViewController1 *viewController1 = [[ViewController1 alloc] init];
ViewController2 *viewController2 = [[ViewController2 alloc] init];
viewController1.delegate = viewController2;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
[self.tabBarController setViewControllers:@[viewController1, navigationController]];

你也可以调查一下NSNotificationCenter

于 2013-07-17T02:27:53.553 回答