2013-03-20 90 views
0

我试图添加一个UIImageView到我的ViewController后,我解雇了一个模态视图控制器。 但由于某种原因[self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo.png"];未将UIImageView添加到显示器,或者至少不可见。下面是我在模态视图控制器中调用的示例代码。addSubView after dismissViewControllerAnimated

/* Modal View Controller */ 
- (IBAction)hideModal:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:^() { 
    TestViewController *gv = [self.storyboard instantiateViewControllerWithIdentifier:@"testView"]; 
    [gv view]; 
    [gv addLogo]; 
    }]; 
} 
/* TestViewController */ 
-(void)addLogo { 
    [self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo.png"]; 
} 

回答

0

完成块与显示的初始视图控制器不一样。
当试图引用视图控制器被模态视图控制器号召下显示下面的代码:

[self dismissViewControllerAnimated:YES completion:^() { 
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController; 
    gv = (TestViewController *)nav.topViewController; 
    [gv displayLogo]; 
}]; 
0

变化从完成块的方法调用

/* Modal View Controller */ 
- (IBAction)hideModal:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    TestViewController *gv = [self.storyboard instantiateViewControllerWithIdentifier:@"testView"]; 
    [gv view]; 
    [gv addLogo]; 

} 
/* TestViewController */ 
-(void)addLogo { 

    UIImage *myImage = [UIImage imageNamed:@"Logo.png"]; 

    UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImageView]; 
    myImageView.frame = CGRectMake(0,0,100,100); 

    if(myImage) 
    { 
     [self.view addSubview:myImageView]; 
     [self.view bringSubViewToFront:myImageView]; 
    } 
    else 
    { 
     NSLog(@"no image found"); 
    } 
} 
+0

我不太明白你的意思。我对Objective-C非常陌生。 – 2013-03-20 19:31:12

+0

lI已添加修改的方法... – 2013-03-20 19:33:00

+0

好的,谢谢。 – 2013-03-20 19:33:36

0

变化

[self addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo.png"]]; 

[self.view addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo.png"] autorelease]]; 

的另一种方式做到这一点,是调用addSubview方法从viewDid出现在你的T中estViewController

+0

它已经有[self.view--它是一个我忘了补充错误。另外,autorelease在iOS 6上不起作用。它已被弃用。 – 2013-03-20 19:52:20

+0

autorelease不会在ios6中弃用,它在ARC中不起作用 – 2013-03-20 20:04:11

+0

是的,我正在使用ARC。 – 2013-03-20 20:08:54