UIViewController传入方法

时间:2013-07-30 11:35:05

标签: ios objective-c

好的,真正的快速问题,如果我想在一个方法中传递视图控制器,这将是正确的方法(显然,两者似乎都有效) -

-(void)fetchedDataN:(UIViewController *)response

-(void)fetchedDataN:(ViewController *)response

编辑:这是澄清事情的完整代码

- (IBAction)nextButton:(id)sender {


 activityN.hidden=NO;
[activityN startAnimating];
[self.view bringSubviewToFront:activityN];

[audioFile stop];
NSLog(@"HELL O");

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   Page04ViewController *viewControl=[[Page04ViewController alloc]initWithNibName:nil bundle:nil]; 

    [self performSelectorOnMainThread:@selector(fetchedDataN:)
                           withObject:viewControl waitUntilDone:YES];
}); }



-(void)fetchedDataN:(UIViewController *)response{


if ([self interfaceOrientation] == UIDeviceOrientationLandscapeLeft) {

    [self presentViewController:response withPushDirection:@"fromTop"];

    //        NSLog(@"landscape left");
}

else if([self interfaceOrientation] == UIDeviceOrientationLandscapeRight)
{
    [self presentViewController:response withPushDirection:@"fromBottom"];

    //    NSLog(@"landscape right");
} }

4 个答案:

答案 0 :(得分:2)

据推测,ViewControllerUIViewController ...

的子类

如果您尝试将UIViewController传递给第二种方法,那将无效(编译器会抱怨),因为它无法保证您传递的实例属于正确的类。< / p>

如果您尝试将ViewController传递给第一个可行的方法,因为编译器可以进行保证。

方法定义的正确性取决于你的意图 - 不知道,两者都不正确,它们只是不同......


对于您更新的问题,如果方法仅显示视图控制器,那么使用UIViewController可以被认为是正确的,因为它是最通用且最灵活的选项。如果你想在将来更改方法以添加需要response作为特定子类的其他功能,那么编译器会告诉你它不能保证类的类型正确。

答案 1 :(得分:0)

这是对的: -(void)fetchedDataN:(UIViewController *)response

然后你可以

-(void)fetchedDataN:(UIViewController *)response
{
    if ([controller isKindOfClass:[ViewControllerSubclass class]])
    {
        //The controller is a ViewControllerSubclass or a subclass of ViewControllerSubclass your code here
    }
    if ([controller isKindOfClass:[MyViewControllerSubclass class]])
    {
        //The controller is a MyViewControllerSubclass or a subclass of MyViewControllerSubclass your code here
    }
}

答案 2 :(得分:0)

这取决于您是否只想传递ViewController和/或它的后代或UIViewController以及所有后代。 UIViewController将更通用,因为(我假设)ViewControllerUIViewController的子类,可能您不再扩展ViewController。一切都取决于上下文,如果您需要使用ViewController中定义的任何特定方法/属性,或者您只想接收任何视图控制器子类UIViewController

答案 3 :(得分:0)

如果你想传递一个UIViewController;使用第一种方法。 我从来没有听说过'ViewController'这个类型。它是你自己的类型?如果您总是希望确保传递的变量是'ViewController'类型,请使用第二种方法。