簡體   English   中英

如何在TabbarController中設置viewControllers的委托?

[英]How to set delegate of viewControllers in TabbarController?

編輯:

我有tabbarcontroller有兩個選項卡,第一個選項卡我有一個viewController,第二個選項卡我有navigationViewController與兩個ViewControllers堆棧與tableView。

TAB1 ---> VC1

Tab2 ---> NVC - > fistVC1 ----推送到-----> secondVC2。

我推送的代碼:

fistVC1.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];

我需要做的是將VC1設置為secondVC2的委托,當選擇secondVC2單元時我想向VC1發送消息。

我怎么能這樣做,請給我一些建議。

我試着打擊:

secondVC2.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;

secondVC2.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;
    }

但代表保持釋放,我在第二個VC2中獲得了nil代表,我不知道為什么。 那么我怎么能得到這個呢?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM