Question

My problem is sample ...But I still don't know why my view won't push to another view ...

First ,I add many views in NSMutableArray

static NSString *titleKey = @"title";
static NSString *viewControllerKey = @"viewController";

- (void)viewDidAppear:(BOOL)animated {
    self.menuList = [NSMutableArray array];

    FirstView *firstView = [[FirstView alloc]initWithNibName:nil bundle:nil];
    [self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                              NSLocalizedString(@"FirstView", @""), titleKey,
                              firstView, viewControllerKey,
                              nil]];
    [FirstView release];


    SecondView *secondView = [[SecondView alloc]initWithNibName:nil bundle:nil];
    [self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                              NSLocalizedString(@"SecondView", @""), titleKey,
                              secondView, viewControllerKey,
                              nil]];
    [SecondView release];

Ok...This how I add viewcontroller in an array ...

I want when I press the cell in the table view ,it will go to the view I want to show

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *targetViewController = [[self.menuList objectAtIndex: indexPath.row] objectForKey:viewControllerKey];
    targetViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [[self navigationController] pushViewController:targetViewController animated:YES];
}

After I press the cell , nothing happen

First I guess could be I didn't set the Array correctly

But When I log the array

It really something inside...

Here is the result :

Log message: MENU LIST (
        {
        title = "FirstView";
        viewController = "<Firstview: 0x5822500>";
    },
        {
        title = "SecondView";
        viewController = "<SecondView: 0x5822870>";
    }
)

I have no idea why the program won't work......

Does anyone have any ideas ???

It just a so simple thing ,still stuck here for 3 hours....

Many thanks :)

Was it helpful?

Solution

First you need to check the type of targetViewController to see if it is giving you the correct ViewController.If it is correct,then

You really should check the self.navigationController value to make sure that it is not nil

I suspect the second thing is the problem.

OTHER TIPS

Well first i have to say, bravo, this is quite a challenging question.

I m and certain that my answer will help or not but just hear me out.

when you are using

UIViewController *targetViewController = [[self.menuList objectAtIndex: indexPath.row] objectForKey:viewControllerKey];

you are assigning a class of type firstview or secondview to uiviewcontroller that my be the problem.

but it is proper as a parent class can take the place of child class (assuming that the 2 class has subclassed the uiviewcontroller).

so you can place a break point on

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

and before the control reaches the end of function place your cursor on targetViewController and check what is its contents.

if it has a firstview or secondview than it will show you the type of variable the firstview and secondview is.

In order to make a push action successfully you need to create a UINavigationController in your application delegate with a root view controller (root view controller = FirstView-> i hope this is a UIViewController).

After you do this, in FirstView you can call

[[self navigationController] pushViewController:targetViewController animated:YES];

in order to push another UIViewController on the stack.

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This class is not intended for subclassing. Instead, you use instances of it as-is in situations where you want your application’s user interface to reflect the hierarchical nature of your content. This navigation interface makes it possible to present your data efficiently and also makes it easier for the user to navigate that content.

Here is the oficial link to the UINavigationController docs, they are useful. Link to UINavigationController documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top