dismissViewControllerAnimated之后的addSubView

时间:2013-03-20 19:27:12

标签: iphone ios objective-c xcode ipad

在解雇模态视图控制器后,我正在尝试向我的ViewController添加UIImageView。 但由于某种原因[self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo.png"];没有将UIImageView添加到显示器,或者至少它不可见。我在模态视图控制器中调用的示例代码如下所示。

/* 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"];
}

3 个答案:

答案 0 :(得分: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");
     }
}

答案 1 :(得分:0)

更改

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

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

另一种方法是从TestViewController中的viewDidAppear调用addSubview方法

答案 2 :(得分:0)

完成块与显示的初始视图控制器不同。
当试图引用正在模态视图控制器下显示的视图控制器时,请调用以下代码:

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