0

我有一个具有4个选项卡的TabBarControllers应用程序。现在,在didFinishLaunchingWithOptions我这样做:获取ViewController的名称

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; 
    lpgr.minimumPressDuration = 5.0; 
    lpgr.delegate = self; 
    [self.window addGestureRecognizer:lpgr]; 
    [lpgr release]; 

这部分工作,并长按手势是在所有的四个选项卡识别。现在,我想传递用户当前正在按下的当前ViewController的名称,以便我可以将其传递到处理长按事件的函数中。

回答

8

你可以试试:

NSStringFromClass([YourViewController class]); 

和当前视图控制器,使用:

NSStringFromClass([self class]); 
+0

但这只会返回AppDelegate类而不是ViewController类。我想在appDelegate中做所有事情,而不是在每个ViewController中放入NSStringFromClass([self class])。 – Diffy 2012-03-02 04:53:42

+0

@Diffy看下面一个... !!!! – Kamarshad 2012-03-02 05:06:46

+0

@Diffy您可以为选项卡VC中的每个VC创建公共方法(例如'selectedViewControllerName:'),然后可以调用所选VC的方法来获取VC的正确名称。 _“我想在appDelegate中做所有事情,而不是在每个ViewController中放置NSStringFromClass([self class]),如果是这样,只需使用第一个ViewController,就可以通过选中VC的TAG来返回名称。 – Kjuly 2012-03-02 05:51:59

1

你应该通过索引(0-3),并有将返回控制器的方法从索引(或将控制器放入公共数组中)。

4

试试下面一(更新)

照你说你已经添加了四个视图控制器上的TabBar控制器。

得到的ViewController从TabBarController如:

UIViewController *current = tabBarController.selectedViewController; 

NSArray *controllerNameArray = [current childViewControllers]; 

的NSLog(@ “的className%@”,[controllerNameArray objectAtIndex:0]);

我希望它真正帮助你...!

+0

谢谢,但它并没有真正给我我想要的东西。我从你的代码中得到了一条线索,我使用了UIViewController * current = tabBarController.selectedViewController; NSArray * className = [current childViewControllers]; NSLog(@“className%@”,[className objectAtIndex:0]);获取课程名称。 – Diffy 2012-03-02 05:41:51

+0

@Diffy,你好,很高兴...... !!!!!你已经得到了你的答案。 – Kamarshad 2012-03-02 05:45:06