0

在我关闭模态视图控制器后,我试图将 UIImageView 添加到我的 ViewController 中。但由于某种原因[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"];
}
4

3 回答 3

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");
     }
}
于 2013-03-20T19:29:53.107 回答
0

改变

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

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

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

于 2013-03-20T19:49:07.630 回答
0

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

    [self dismissViewControllerAnimated:YES completion:^() {
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    gv = (TestViewController *)nav.topViewController;
    [gv displayLogo];
}];
于 2013-03-20T21:15:22.380 回答