[Objective-C]iPhone/目标-c-获取以前的 ViewController 从子视图吗?

发布时间: 2016/9/23 2:18:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

如何访问以前的视图控制器在子视图?因为我能够执行的操作,但我只是不能使用 self.[my main view controller]

这是我的代码,用于测试目的︰

PhotoViewController.m

-(IBAction)likeButton:(UIButton *)sender
{
    //this part works
    NSString *num = @"2";
    self.label.text = [NSString stringWithFormat:@"%@ + %@",
                                                 self.label.text,
                                                 num];

    //this part doesn't work
    //switch over to the third view to see if it worked
    self.tabBarController.selectedIndex = 0;
}

我有 UITabBarController 和其视图控制器之一有 UIScrollView 。里面的 UIScrollViewPhotoViewController 对象。

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch
    MyTabBarViewController *vc2 = [[MyTabBarViewController alloc] init];
    SecondViewController *vc3 = [[SecondViewController alloc] init];
    controller = [[DemoAppViewController alloc] init];
    controller.view.frame = CGRectMake(0, 20, 320, 460);

    controller.title = @"Intro Screen";
    vc2.title = @"Explore";
    vc3.title = @"Send a Pic";
    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = [NSArray arrayWithObjects:controller, vc2, vc3, nil];
    [controller release];
    [vc2 release];
    [vc3 release];

    [self.window addSubview:tbc.view];
    [self.window makeKeyAndVisible];

  return YES;

}

此外,我 TabBarViewController.m //not 我实际的 UITabBarController 虽然,措辞是令人困惑

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    arrayCount = [array count];
    scroller.delegate=self;
    scroller.pagingEnabled=YES;
    scroller.directionalLockEnabled=YES;
    scroller.showsHorizontalScrollIndicator=NO;
    scroller.showsVerticalScrollIndicator=NO;

    //should have an array of photo objects and the number of objects, correct?
    scrollWidth = 0;
    scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);

    for (int i = 0; i < arrayCount;i++) {
        PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];        
        UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:CGRectOffset(scroller.bounds, scrollWidth, 0)];
        CGRect rect = scrollImageView.frame;
        pvc.view.frame  = rect;
        pvc.label.textColor = [UIColor whiteColor];
        id individualPhoto = [array objectAtIndex:i];
        NSLog(@"%@",individualPhoto);
        NSArray *keys=[individualPhoto allKeys];
        NSLog(@"%@",keys);
        NSString *imageURL=[individualPhoto objectForKey:@"source"];
        NSURL *url = [NSURL URLWithString:imageURL];
        NSData  *data = [NSData dataWithContentsOfURL:url];
        pvc.imageView.image = [[UIImage alloc] initWithData:data];
        pvc.label.text = [individualPhoto objectForKey:@"id"];
        [scroller addSubview:pvc.view];
        [scrollImageView release];
        //[pvc release];
        scrollWidth += scroller.frame.size.width;
    }

    if (arrayCount > 3) {
        pageControl.numberOfPages=3;
    } else {
    pageControl.numberOfPages=arrayCount;
    }
    pageControl.currentPage=0;
}

解决方法 1:

尝试定义这种方法在你 PhotoViewController :

+ (YOURTABBARCONTROLLER*)parentTabBarController:(UIResponder*)view {
    id nextResponder = nil;
    id v = view;
    while (nextResponder = [v nextResponder]) {
               NSLog(@"Found Responder: %@", nextResponder); //-- ADDED THIS
       if ([nextResponder isKindOfClass:[YOURTABBARCONTROLLER class]])
          return nextResponder;
        v = nextResponder;
    }
    return nil;
 }

它会遍历响应链并返回找到的给定类型的第一个控制器。取代 YOURTABBARCONTROLLER 与你实际的标签栏控制器类,你应该能够︰

-(IBAction)likeButton:(UIButton *)sender
{
    //this part works
    NSString *num = @"2";
    self.label.text = [NSString stringWithFormat:@"%@ + %@",
                                             self.label.text,
                                             num];

    [PhotoViewController parentTabBarController:self.view].selectedIndex = 0;
    // self.tabBarController.selectedIndex = 0;
}

更新

-(IBAction)likeCommentButton:(UIButton *)sender
{
    //code goes here
    TypeSomethingViewController *typeSomethingViewController = [[TypeSomethingViewController alloc] init];
    typeSomethingViewController.delegate = self;

    [self presentModalViewController:typeSomethingViewController animated:YES];
    [typeSomethingViewController release];
}

-(void)typeSomethingViewController:(TypeSomethingViewController *)controller didTypeSomething:(NSString *)text
{
    //NSLog(@"response: %@", controller);

    NSString *commentID = self.label.text;
    for(UIViewController *controller in [PhotoViewController parentTabBarController:self.parentViewController.view].viewControllers)
    {
        if([controller isKindOfClass:[DemoAppViewController class]])
        {
            DemoAppViewController *davc = (DemoAppViewController *)controller;
            //[davc commentPicture:commentID :message];
            [davc likePicture:commentID];
        }
    }
    [PhotoViewController parentTabBarController:self.view].selectedIndex = 0;

    [self dismissModalViewControllerAnimated:YES];    
}
赞助商