Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
can't wrap my head around this :confused:

1. set up appDelegate and 2 view controllers. adds the first view controller on launch

Code:
- (void)applicationDidFinishLaunching:(UIApplication *)application
	{	
	[window addSubview:firstViewController.view];
	[window makeKeyAndVisible];
	}

2. inside the firstViewController.view, there is a button that will swap out the firstViewController.view for the secondViewController.view on the main window.

the swapping code will be in the appDelegate class

Code:
- (void)swapViews
	{
	[window insertSubview:secondViewController.view aboveSubview:firstViewController.view];
	}

the problem is that i don't know how to call the appDelegate class from my firstViewController class

Code:
- (IBAction)buttonPressed
	{
	[[COLOR="Red"][I]whatGoesHere[/I][/COLOR] swapViews];
	}

i've tried making instance variables of the appDelegate class inside my firstViewController class to call it that way, but it just doesn't get called (or crashes). the appDelegate class has instance variables of both the view controllers in order for it to know about them and call them, but it seems i can't include instance variables of the appDelegate within the 2 view controllers.

in Beginning iPhone Development (chapter 6: Multiview Applications) talks about this. if anyone is familiar with this chapter, do you know how to make this project work by removing the toolbar completely, and causing the views to switch by the buttons inside each of the views? here is the link to the book's source code, and the project is "06 View Switcher".
 
maybe you should post the code where you assign the appDelegate to an instance variable. because I don't see a reason why that shouldn't work ^^
 
You can always access the app delegate from anywhere by using the following:

Code:
id delegate = [[UIApplication sharedApplication] delegate];

However, as you can tell from the above code, the return type is id and it won't know anything about your app delegate's properties unless you tell it what it is explicitly with a cast:

Code:
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

However, ask yourself why you want to do this; in general its best to avoid using global objects unless you really have to. There are frequently better ways of designing your application.

In this case, I ask you why does your app delegate need to know about the second view controller, when it's the first view controller that needs to use it? Why can't your first view controller instantiate the second controller instead?

It seems to me that the only reason you've made the app delegate responsible for doing the view swapping is because it already knows about the application window so it can insert the new view over the current one. Have you considered presenting the second view as a modal view instead?

In other words, in your first view controller:

Code:
- (IBAction)buttonPressed
{
  SecondViewController *c = [[SecondViewController alloc] init]; // or whatever the appropriate initialize method is
  [self presentModalViewController:c animated:YES]; // or NO if you don't want to animate it
  [c release];
}

In this case, a new instance of your second controller will be created each time the button is clicked; if you only need one instance, lazy-load it and store it in an instance variable:

Code:
- (IBAction)buttonPressed
{
  if(secondViewController == nil) { // this is the instance var
    secondViewController = [[SecondViewController alloc] init]; // don't forget to release in your dealloc method
  }
  [self presentModalViewController:secondViewController animated:YES];
}

Your second view controller can dismiss itself to reveal the first view again by calling:

Code:
[self dismissModalViewControllerAnimated:YES];
 
attached is my small sample project using the delegate to swap 2 views over the main window... can't get it to work. the methods i'm targeting in the delegate method from the view controllers are simply being ignored completely, but i receive no compiler warnings or errors.
 

Attachments

  • SwapViews.zip
    24.4 KB · Views: 75
So, Luke Redpath gave you some good ideas there you might want to look at. At the very least look at how you can get the current application's delegate from anywhere in the app.

In your case right now you have a property called appDelegate in each of your view controllers. The problem is you never actually set that property anywhere.

You could declare those two properties as an IBOutlet and then connect each of them to your app delegate in IB.

Or instead just ask the current application for its delegate when you need to send it a message.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.