Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6136377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:35:45+00:00 2026-05-23T17:35:45+00:00

How do I access a previous view controller while in a subview? Because I’m

  • 0

How do I access a previous view controller while in a subview? Because I’m able to perform actions but I’m just not able to use self.[my main view controller].

This is my code, for testing purposes:

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;
}

I have a UITabBarController and one of its view controllers has a UIScrollView. Inside of the UIScrollView is a PhotoViewController object.

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;

}

Also, my TabBarViewController.m //not my actual UITabBarController though, wording is confusing

- (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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:35:45+00:00Added an answer on May 23, 2026 at 5:35 pm

    Try defining this method in your 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;
     }
    

    it will traverse the responder chain and return the first controller of a given type that is found. Replace YOURTABBARCONTROLLER with your actual tab bar controller class and you should be able to have:

    -(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;
    }
    

    Updated

    -(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];    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple database with web access. I have previous experience with
Access can open DBF (dBase) files, but instead of physically converting the data into
MS Access appears to support nulls in code, but I can't for the life
I am trying to specify the access to a certain django view only to
I'm refactoring a little bit of C# data access code from a previous developer
Following my previous question: Lambda expression to access a property of an object that
Is it possible to access the previous element generated in a list comprehension ?
Using MS Access database I want to display today record min (time) and previous
I have a Nav controller that starts at a table view. Each row pushes
How can I detect whether or not an Image View has a picture in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.