views:

2491

answers:

2

I'd like to have a view appear when the user clicks a button. The hierarchy I have looks like this:

MainWindow

-UIView
--ScrollView
---ScrollView.pages = UIViews
----UIView (from above assignment)
----TextView
----InfoButton

pages is an NSMutableArry of pageController objects. These hook to a nib. These nibs are the pages that user flicks through in the scroll view.

The InfoButton click is wired up like this:

- (IBAction) infoButton_click:(id)sender{
topView topViewViewController *topView = [[topViewViewController alloc] initWithNibName:@"TopView" bundle:[NSBundle mainBundle]];
//[self.navigationController pushViewController: topViewView animated:YES]; 
//[self.view addSubview: topViewView.view];
[super.view addSubview: topViewView.view];
[topViewView release];
}

InfoButton is on one of the pages in the ScrollView. I've commented out different code that has been tried. None of it adds the view. Nothing happens. Is there a way to get TopView as the top view in the hierarchy?

+1  A: 

Is your goal to add the view as a subview, or to slide on a new view using the navigation controller? I'm going to assume the latter for the moment.

- (IBAction)infoButton_click:(id)sender
{
    TopViewController *topViewController = [[TopViewController alloc] initWithNibName:@"TopView" bundle:nil];
    [self.navigationController pushViewController:topViewController animated:YES]; 
    [topViewController release];
 }

This is correct if you actually have a navigationController. Make sure you actually do. When "nothing happens" in Cocoa, it usually means something is nil. You should check in the debugger or with NSLog() to see if any of these values are nil. It is possible (even likely), that your parent has a navigationController, but you do not.

Classes should always have a leading capital. Do not create a variable called "view" that is of class "UIViewController". This is a sure path to suffering. Objective-C is a dynamic language with limited compiler checks on types. Naming things correctly is critical to effective programming in ObjC.

Rob Napier
This is on the iPhone (Cocoa Touch). It isn't a navigation controller. It is a view based app. I have tried the code you show, as noted in my OP, but it doesn't work.
4thSpace
So what is your actual goal? How do you wish "TopView" to appear? Should it be part of the existing scrollview (and if so, where?), should it slide on from below or do you want to slide to the right to see it? It's not clear here what ScrollView.pages is in your diagram. Is "super" this "pages" or is it the actual UIScrollView?
Rob Napier
I want to press the info button and have another view appear. Would be nice if it could appear as a flip like in the Star Trek apps. But sliding in is fine. Once that view appears, the user shouldn't be able to slide/swipe anymore. It's a settings/info view. So I'm thinking it probably isn't part of the UIScrollView anymore. User will then click Done and go back to the scroll view. I've edited the OP to describe what pages is. Not sure on the "super" question. Maybe these clarifications will help. Please let me know. Thanks.
4thSpace
I'm not familiar with the Star Trek apps, but what you are describing is called a modal view controller: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/PresentingModelViewControllers/PresentingModelViewControllers.html
Rob Napier
Thanks on the modal view (and hatfinch). That works great.
4thSpace
+1  A: 

Based on your comment to a previous answer, you want to present a modal view. You do this by creating a new view "modalView" and calling [topView presentModalViewController:modalView animated:YES].

In a future version of the iPhone OS, which of course I would be unable to comment upon if it were under NDA, you might be able to present a modal view controller with a flip transition by setting a property on the view controller to be presented, which would probably be called modalTransitionStyle or somesuch.

hatfinch