MKMapView keeps resetting back to the world view - iphone

Original question is below.
I fixed my problem. I had the same view controller set for the tableview and the mapview. AND I had all of my mapView init in viewDidLoad. These both together made the following problems for me:
1) When the tab bar controller was presented modally and I received a memory warning the viewDidUnload was called (which I did nothing in before). When the modal tab bar controller was dismissed and viewDidLoad was called, it reset my mapView.
2) When the tab bar controller was presented modally and I received a memory warning before moving to the tab with the view controller and then switched to that tab, my viewDidLoad was called in my mapView's controller which reset my map, AND it didn't reset the tables properties so that my tableView would not work.
Much thanks to Anomie for helping me debug this issue, and gain a better understanding of how these pieces are all connected. Things work great now, and my App has a better design.
Original Question:
I have an application that has a mapview and when I present a tab bar view controller modally, every once in a while when I cancel the modal view controller the map resets back to the world view like a reset button was pressed on it. A few details:
This NEVER happens on the simulator
I notice memory warnings around the time this happens on my device
I do not do anything when memory warnings happen in my app, so nothing should have touched the mapview
Also, my tableview in my modal view controller sometimes shows up blank as well (aroudn the time of memory warnings). When it comes up blank, the methods normally called to get the number of rows, sections, and data are not called at all. Upon cancelling the modal view controller and re-opening it, the data is there fine, so the data is not getting erased..
Code I used to create the tab bar controller, picker and table:
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// Display only a person's address(es)
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonAddressProperty],
nil];
picker.displayedProperties = displayedItems;
UITabBarItem *peoplePickerTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
picker.tabBarItem = peoplePickerTabBarItem;
UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
tvc.tableView.delegate = self;
tvc.tableView.dataSource = self;
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:tvc];
UIBarButtonItem *uibbiCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancelTable)];
tvc.navigationItem.rightBarButtonItem = uibbiCancel;
tvc.title = #"Recents";
UITabBarItem *nvcTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
nvc.tabBarItem = nvcTabBarItem;
tbc = [[UITabBarController alloc] init];
NSArray *sections = [[NSArray alloc] initWithObjects:picker, nvc, nil];
[tbc setViewControllers:sections];
[self presentModalViewController:tbc animated:YES];
[nvcTabBarItem release];
[uibbiCancel release];
[tvc release];
[peoplePickerTabBarItem release];
[picker release];
[nvc release];
[sections release];
[tbc release];

For your map view, what is probably happening is this: When a UIViewController is not being actively displayed (e.g. it is in a non-displayed tab of a UITabViewController, not the visible view in a UINavigationController, or is hidden by a modally presented view controller), it may release its view if a memory notification occurs. It will then recreate the view when needed.
For your table view, I'm not sure. Are you assigning the data source and delegate to the table view in the code that creates the containing view controller, rather than doing so in the view controller's viewDidLoad method?

Related

No effect on setViewController:animiated:?

Basically I have
ViewControllerA *aVC = [[ViewControllerA alloc] init];
ViewControllerB *bVC = [[ViewControllerB alloc] init];
UITabBarController *tabBarVC = [[UITabBarController alloc] init];
[tabBarVC setViewControllers:[[NSArray alloc] initWithObjects:aVC, bVC, nil] animated:YES];
Now I can see the two tabs on the tabBarController but when I switch from one tab to another, I can't see any effects, neither on simulator or on real device. From the documentation I should be able to see fading right? Did I miss anything?
If you pass YES to setViewControllers:animated:, UITabBarController will animate the insertion of the the new tab bar items in the tab bar. It doesn't animate the transition between view controllers if the user then switches from one tab to another.

iPhone toolbar for entire app

I have my app base on navigationController. So i set the toolbar visible for some views and for others i didnt calling setToolbarHidden:NO or YES.
first question, this goes in viewWillAppear method ?
Then in my appDelegate, I put one item on the toolbar but is not being show.
can someone show me how can I use delegate protocol here so each view know what to do when a item is pressed??
my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//create itemViewcontroller
EventosViewController *itemsViewController = [[EventosViewController alloc] init];
//create UINavigationcontroller, stack only contains itemviewcontroller
navController=[[UINavigationController alloc] initWithRootViewController:itemsViewController];
//navController will retain itemviewcontroller, we can release it
[itemsViewController release];
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem,nil];
//release buttons
[systemItem1 release];
[flexItem release];
//add array of buttons to toolbar
[navController.toolbar setItems:items animated:NO];
//set navController's view in window hierarchy
[[self window] setRootViewController:navController];
[navController release];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
thx in advance!
The toolbar us hidden by default. The toolbarItems should be stored in the respective view controller, not the navigation controller.
From the documentation:
Displaying a Toolbar
In iOS 3.0 and later, navigation controller objects make it easy to provide a custom toolbar for each screen of a
navigation interface. The navigation controller object now manages an
optional toolbar in its view hierarchy. When displayed, this toolbar
obtains its current set of items from the toolbarItems property of the
active view controller. When the active view controller changes, the
navigation controller updates the toolbar items to match the new view
controller, animating the new items into position when appropriate.
The navigation toolbar is hidden by default but you can show it for
your navigation interface by calling the setToolbarHidden:animated:
method of your navigation controller object. If not all of your view
controllers support toolbar items, your delegate object can call this
method to toggle the visibility of the toolbar during subsequent push
and pop operations.
you can keep the toolbaritems like this
ThemeDetailViewController *themeDetail = [[ThemeDetailViewController alloc] init];
[self.navigationController pushViewController:themeDetail animated:YES];
themeDetail.toolbarItems = self.parentViewController.toolbarItems;

How to add a 2nd view as a Tab Bar View (iPhone SDK)

So before I've managed to work with TabBarViewControllers and create an application using them. However every time I do so the view acts as my main view. This time around I want my TabBarView to be my second view in my application
E.g
First window has a bunch of buttons, when I click one of these buttons I want the second view to show up. This view includes a TabBarViewController.
The farthest I've gotten is to have the button show a view but for some reason it won't show my TabBar view!
Here's the code for my button
- (IBAction)showEvents:(id)sender {
EventsViewController *controller = [[EventsViewController alloc] initWithNibName:#"EventsView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
}
Any of you guys able to help?
Can't you just in the EventsViewController add the following code in viewDidLoad:
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects: vc1, vc2, ..., nil];
Anyway, I found a solution and it was actually quite simple. After creating the Outlet for the TabBarController and linking it together with File's Owner all I had to do was add
self.view = tabViewController.view;
On the viewDidLoad method

iPhone - Loading a new view and a deallocated object

So I feel like a serious rookie right now, but I have a problem I can't seem to figure out. I have a barebones app, with literally nothing in it except a login screen and a second view containing a tableview. When I add the second view after logging in (I have done this like 4 times before...), the table view goes through its delegates and appears that it's going to load, but something happens. I have enabled my NSZombies, and it appears to be deallocating the new view, right before it appears.
After tracing through it, and building up again piece by piece, it appears to happen after I wire the table to the view as the datasource/delegate in IB. I have set the view as a UITableViewDelegate, and the methods indeed get fired. Does anyone have any idea what might be causing this behavior?
Have you added the 'second'view to an exisitng view using addSubview: or added it to some form of UINavigationController or UITabBarController? When you do this it will automatically increase the retain count and whatever code you have releasing the view won't cause is to be deallocated.
In my AppDelegate application:didFinishLaunchingWithOptions I have something like;
LoginViewController *login = [[LoginViewController alloc] init];
[login setDelegate:self];
loginNavController = [[UINavigationController alloc]
initWithRootViewController:login];
[window addSubview:[loginNavController view]];
And then once login has occured (and succeeded using a protocol/delegate to send the message back to AppDelegate) I call this code;
UIViewController *newView1 = [[UIViewController alloc] init];
UIViewController *newView2 = [[UIViewController alloc] init];
UIViewController *newView3 = [[UIViewController alloc] init];
myTabBarController = [[UITabBarController alloc] init];
myNavController = [[UINavigationController alloc]
initWithRootViewController:newView1];
// nav controller now retaining
[newView1 release];
NSArray *viewControllers = [NSArray arrayWithObjects:myNavController,
newView2,
newView3,
nil];
[myTabBarController setViewControllers:viewControllers animated:YES];
[[myTabBarController view] setFrame:[[UIScreen mainScreen] applicationFrame]];
[window addSubview:[tabBarController view]];
// tab bar controller now retaining
[newView2 release];
[newView3 release];
// remove login from application
[[loginNavController view] removeFromSuperview];
The AppDelegate has the following declared in the header file;
LoginViewController *loginViewController;
UITabBarController *myTabBarController;
UINavigationController *myNavController;
In the dealloc method for the AppDelegate these are released.
This gives me my login page and then when that has processed my views with a top nav all controlled using the bottom tab bar.
Hope this helps in some way.
You have either too many release (or autorelease) calls - or not enough retain calls - in your view loading/transitioning code, but it's impossible to be more specific without seeing that code.
What's probably happening is the autorelease pool is being flushed between your view loading and your view being shown, and that's what's leading the behaviour you describe.

Three20 TTLauncher Issues

So, I'm having some issues with my implementation of the Three20 TTLauncherView. I am using their code, not a fork (although I have heard of rodmaz's version), and I can't get it to work properly. This is what my app looks like.
alt text http://img709.imageshack.us/img709/8792/screenshot20100715at409.png
I removed the icon image, that's not the issue. The issue is, at the top there is no Navigation bar at all, and I believe also causes the white strip at the bottom, which appears to have the same dimensions as a Nav Bar. I've spent quite a while looking through their code and can't figure this out at all. It looks like their Navigation bar (as seen in their Catalog example app) stems from the TTTableViewController, or something further up. However, my app starts like the Facebook app does, not into a table, but into the TTLauncherView. So... how do I get the Navigation bar into my TTLauncher view, if it goes "App Delegate -> TTLauncherView Subclass"
Thanks for your help!
Edit:
Added the code I used. I put this in my app delegate, wrapping my first view with the UINavigation Controller, and it worked just as I wanted!
MainViewController *aController = [[MainViewController alloc] initWithNibName:nil bundle:nil]; //my Main view
self.mainViewController = aController;
[aController release]; //release for Memory Management
self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
UINavigationController *navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:self.mainViewController animated:NO]; //Gets the main view on the screen
[window addSubview:navigationController.view];
You simply wrap the view with a navigation bar before you push the new view. As an example, here is a snippet of my code where I present a modal view controller with a navigation bar.
- (IBAction) showNewNavView: (id) sender
{
// Present it as a modal view and wrap the controller in a navigation controller to provide a navigation bar for the Edit and Save buttons
ModalViewController *addController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
addController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[addController release];
}
If you want to add any buttons or set the title of it, you need to do that in the viewDidLoad method of the view that you are pushing (i.e. your TTLauncher view)