Setting tabBarController.selectedIndex/selectedViewController when it's a UINavigationController - xcode

I've got 5 views in my tabBarController and all of them are embedded in a separate navigationcontroller, i.e. every view has it's own navigationcontroller. I did this to make it easier to push segues, I know it's probably not the best solution but it works fine. Now to my question:
I'm trying to set the initial view with the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBarController.selectedIndex = 2;
return YES;
}
However it's not working at all, the app simply starts and index 0 (the left most view).
I've searched through thorough threads like this and tried many different ways to solve this without any success...
Closest I got was when I in MainStoryboard_iPhone.storyboard checked the box "Is initial view controller" in the viewcontroller I want to start with. This way the I got the correct starting viewcontroller but the tabbar wasn't showing.

Since you're using storyboard, do this :
1) Give your tabBarController a storyboard identifier (say tbc);
2) In your appDelegate DidFinishLaunching, do this ::
UITabBarController *tbc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"tbc"];
[tbc setSelectedIndex:1];
[self.window setRootViewController:tbc];
[self.window makeKeyAndVisible];
return YES;
PS : This is just one of many ways to make it work

Related

Objects releasing on iOS 6 but not in iOS 5

I am having a really weird issue when testing on my 1st gen. iPad (running iOS 5).
I have a UIView that I use as a property (with retain). I nil the property in the parent view's dealloc method. Pretty basic stuff. It works perfect on my iPad 3 running iOS 6, but doesn't get released on my 1st gen.
Any ideas what might be going on?
I'm not using ARC.
If you're retaining it, you have to release it. You can't just nil the instance variable.
So if you're property looks like this:
#property (nonatomic, retain) UIView *myView;
You're dealloc would either look like this:
- (void)dealloc
{
[myView release], myView = nil;
[super dealloc];
}
Or this:
- (void)dealloc
{
[self setMyView:nil];
[super dealloc];
}
Or this:
- (void)dealloc
{
self.myView = nil;
[super dealloc];
}
And your property will properly get released--unless something else is retaining it.
So I figured this out. It seems to be a bug in the iOS 6 SDK or maybe I just don't understand it. I have a UIViewController that presents another vc via presentViewController:animated:completion: —If I dismiss the presented vc then it releases and subsequently all subviews are removed and all is well.
However, if while the presented vc is showing, I remove/destroy the parent vc, the presented vc is deallocated but, its subviews are not told to removeFromSuperview; This doesn't show up as a leak in instruments, BUT it does prevent the subviews from deallocating.
This does not happen on iOS 6, thus I suspect this is a bug in iOS 5. Everything releases/deallocates as one would expect on iOS 6.
If someone has an explanation, or a better understanding of this, I would love to reward the answer to them instead of myself.
A view controller isn't responsible for removing its view from the superview when the view controller is dealloc'ed. The view controller is just responsible for releasing its own reference to it.
For example: you can create a view controller, ask for its view, then add that view to another view and throw away the view controller. In that case, you're just using the view controller as a view builder.
I'm not sure why the behavior is different in iOS 6, but would love to know.

NSManagedObjectContext starts with value and become null

I have just started developing iPad apps and I'm struggling with an issue for too long now, so I've decided to go for help.
I have an application for iPad using storyboard and started as a Tabbed application using CoreData. So the issue is, my NSManagedObjectContext starts with a value but when I move to another tab, the managedObjectContext becomes null.
Don't know what to do. Some help would be greatly appreciated.
Thanks
Elkucho
I'm slightly confused...
self.window.rootViewController
should return your instance of UITabBarController, which should not respond to setManagedObjectContext: and therefore should crash.
With this in mind what you need to do it
Get the tabBarController
Cycle through the viewController's that the tabBarController manages
Pass them the managedObjectContext
for (id viewController in self.window.rootViewController.viewControllers) {
[viewController setManagedObjectContext:self.managedObjectContext];
}
Edit
I've taken a quick look.
You rarely need to subclass UITabBarController and you don't really need to in this case.
What you want to do is just get the managedObjectContext to each of the viewControllers in the tabBarController, the tabBarController itself does need to know about it.
I changed your application:didFinishLaunchingWithOptions: to the following and it worked the way it was supposed to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (id)self.window.rootViewController;
for (id viewController in tabBarController.viewControllers) {
[viewController setManagedObjectContext:self.managedObjectContext];
}
}

Xcode 4 - approved way of switching between views in Storyboards

I have a Storyboarded app and need to switch between views. E.g. when a calculation is complete I want to load up a new view and display the answers...
So what I an trying to do is:
IBAction (Button pressed to calculate answer) {
Do the Maths
Jump to Results View
}
I have read several ways of doing this but none seem to work.
I am trying approaches like:
SRViewController *second = [[SRViewController alloc] initWithNibName:#"SRViewController" bundle:nil];
[self.navigationController pushViewController:second animated:YES];
What do I need to do?
use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method to complete your required work
for more see this

How to navigate b/w three views using IBAction button method and round rectangle button in Xcode?

Opening Xcode 4.2, I chose a empty application template. All im trying to do is make three views, which can be navigated through the help of one button inserted [Round Rectangle Button].
I have made 3 Views with three buttons within them which are
1)Next
2)Next
3)Root
I am relatively new to Xcode. I have imported all the Views into the app delegates. I know i have done everything right. But I think my code for button is wrong.
I inserted a round rectangular button through the object library.
I went in FirstViewController.h, put in -(IBAction)nextbuttonclicked;
I then go in FirstViewController.m, go down, -(IBAction)nextbuttonclicked {
SecondViewController *SVC = [[SecondViewController alloc] init];
[self.window removeFromSuperview];
AppDelegate *appD = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[appD.window addSubview:SVC.view];
}
The error i get is: Thread 1: Program received signal: "EXC_BAD_ACCESS" while
"return UIApplicationMain (argc, argv, nil) NSStringFromClass ([[AppDelegate class]));"
is highlighted hence the thread is coming from here
Someone please help?
Study navigation controller - http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
and use navigation controller's Push and Pop properties for navigations.

UIImageView shifted when I click my back button

I've got a small project with two UIImageView-based nib files.
When the main view loads, it looks correct.
To switch between views, I'm using code like this (for example, this is to go to second view)
-(IBAction) secondClicked:(id)sender {
SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:main animated:NO];
[second release];
}
When the new view is shown, it's shifted sideways about 10% of screen. Using similar code to go back to main results in the main view being shown similarly offset.
I'm in landscape mode, if that makes a difference.
How can I display the views so they're correctly aligned?
Ok it makes sense now that I looked more carefully. Why are you presening a view controller modally when you wish to go back? How did you showed this viewController initially? If it was through a navigation controller you should pop the view
[self.navigationController popViewControllerAnimated:YES];
If it was presented modally you should just dismiss it!
[self dismissViewControllerAnimated:YES];

Resources