xcode - switch between more than 2 view controllers - xcode

I have several .xib files (page1, page2 ...) and want to switch between them using buttons (as a navigation bar)
I am using
page1 *second = [[page1 alloc] initWithNibName:nil bundle:nil];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:second animated: YES];
page2 *second = [[page2 alloc] initWithNibName:nil bundle:nil];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:second animated: YES];
and so on...
I learned that this consumes a lot of memory because I am presenting a view over and over again and after switching back and forth the app sometimes crashes.
So, question is what can I do?
I tried this:
[self dismissModalViewControllerAnimated:YES]
but after dismiss it returns to the previous page and I can not navigate to another one.
Probably I used a stupid approach, however, I've done a few apps that way and don't want to change the controller completely.
Thank you for your help!

I think you have to create IBActions from that 2 blocks of code an link that IBActions to the right button.

Related

Adding ViewController to existing UINavigation stack

How to present modal ViewController on top of UINavigationController without using initWithRootViewController, just with adding it to the existing navigationcontroller stack?
my code is:
TableViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"TableView"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentViewController:navi animated:NO completion:nil];
edit: what i actually want is to do like: "vc1 push vc2 modal vc3" and than use "poptoroot...to vc1". But the initWithRootViewController (vc3) is ruining it.
If you want to change the stack of navigationController. Use :
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); // If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.
It will help.

How to I launch a view controller from a tap gesture

Apologies if this is a basic question but I am new to Xcode and have a storyboard app and in the storyboard (with no segue) I have a view controller with an embedded map view.
On the storyboard screen I have an image with a tap gesture linked, I have tested the tap and it works (NSLog) but I want to know how to launch my mapview view controller and also zoom to an x&y.
My code currently has
-(IBAction)handleMapTap:(UIGestureRecognizer *)sender {
NSLog(#"Tapped");
}
& I have tried;
MMMapViewController *mapViewController = [[MMMapViewController alloc] initWithNibName:#"MapView" bundle:nil];
[self.navigationController pushViewController:mapViewController animated:YES];
My view controller has a class set as MMMapViewController I have given the view controller a storyboard id of MapView (if it's required?).
I've read a lot of stackoverflow articles but can't seem to find an answer. If anyone can help I would be really grateful.
-(IBAction)handleMapTap:(UIGestureRecognizer *)sender
{
MMMapViewController *mapViewController = [[MMMapViewController alloc] initWithNibName:#"MapView" bundle:nil];
//[self.navigationController pushViewController:mapViewController animated:YES];
[self presentModalViewController:mapViewController animated:YES];
[mapViewController release];
}
It would probably help to know what self is, if it is indeed a UIViewController, then
I would make sure that self.navigationController is not nil.
You actually have the right code, as far as I can tell, but depending on your scenario, you could get away with presenting the mapView as a modal view controller.

Where do I put the code for changing the nav bar color in xcode 4.3

Ok, it's 3 am atm and I haven't a clue where I put this:
[navigationController.navigationBar setTintColor:[UIColor redColor];
If you would please post the whole .m/.h files that would be great. Also, do I connect anything using segues or outlets? And when you create the .h/.m files do I need UINavigationController or similar selected or just the normal UIViewController? Thanks.
Update: Nevermind I got it, thanks though. Below is the code for others having my issue.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor lightGrayColor]];
}
Basically just add on to what's already there.
I feel stupid lol.
You can set that value right after you init your UINavigationController, i.e:
UINavigationController *controller = [[UINabvigationController alloc] initWithRoot...
[controller.navigationBar setTintColor:[UIColor redColor]];

How do I put the name of the IBAction pressed on my main view controller in a NSString on my modal view?

I have about ten IBActions on my main view that go to the same modal view and I need to know how to see what IBActon was pressed to get there and put that in a NSString on the modal view.
Here's the code that sends you to the modal view. The only difference in the IBActions is the name. Like playMovie: and playMovie2: and so on.
-(IBAction)playMovie:(id)sender{
VideoSubViewController *subView = [[[VideoSubViewController alloc] init] autorelease];
[subView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:subView animated:YES];}
You could create an init* on the modal view that takes that information. The usage would be:
VideoSubViewController *subView =
[[[VideoSubViewController alloc] initForAction:#"playMovie"] autorelease];
You then store that information in the view instance in order to use it.

Interface Builder: Failing to display TabBarController when calling from TableViewController

A buddy of mine asked for a quick sample of code for an app skeleton that would use a TableView to call a TabView. I estimated an hour.
After more hours than I want to admit messing around in IB, I gave up and implemented the following code.
Can anyone tell me how to do this in IB? I was careful (I thought) to make all the right connections, but no go. I even had another (working) app where I went through and step-by-step made the same connections. I got errors about "Changing the delegate of a tab bar managed by a tab bar controller is not allowed..." (This when I connected the TabBar's delegate to the File's owner, even though another app was working fine with that setting)
Until I wrote this code, I never got the tabbar view, only the view that came with the view xib... (I tested by putting a label on the view).
Thanks in advance...
UITabBarController *tabBarController = [[[UITabBarController alloc] initWithNibName:nil bundle:nil] autorelease];
NumberOneViewController *numberOneViewController = [[[NumberOneViewController alloc] initWithNibName:#"NumberOneViewController" bundle:nil] autorelease];
NumberTwoViewController *numberTwoViewController = [[[NumberTwoViewController alloc] initWithNibName:#"NumberTwoViewController" bundle:nil] autorelease];
NumberThreeViewController *numberThreeViewController = [[[NumberThreeViewController alloc] initWithNibName:#"NumberThreeViewController" bundle:nil] autorelease];
NumberFourViewController *numberFourViewController = [[[NumberFourViewController alloc] initWithNibName:#"NumberFourViewController" bundle:nil] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:numberOneViewController, numberTwoViewController,
numberThreeViewController, numberFourViewController, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
self.view = tabBarController.view; in the viewDidLoad method of the TabBarController delegate class fixed it...
Ah well, surely someone else will run into the same thing and hopefully this will help them...

Resources