IOS 5 SDK and Segues - ios5

I am creating a project with Xcode 4.2 and using it's storyboard. In one of the views I have a button that a user will tap and it will perform some calculations. If the calculations are correct I need to display the view that they just came from. I am currently using a Navigation Controller. When the app starts it loads View 1. When I choose an option it loads View 2. If I click the 'back' button in the toolbar it loads View 1 with left animation.
In my IBAction method for the View 2 calculation I have the following snippet.
[self performSegueWithIdentifier:#"BackToView1" sender:sender];
But the problem is it loads View 1 using the left animation when I need it to load the right.
Would that be a custom segue? Am I missing something?
[Edit]
I just noticed something as well. When View 1 loads from my IBAction it appears it is initializing the AppDelegate again, whereas the 'back' button does not. I need to not initialize the AppDelegate since I am loading a data object at the start of the app. Calling init again kills my object.

You could try using the navigation controller to pop back. This will probably produce the effect you are after.
[self.navigationController popViewControllerAnimated:YES];

Related

Trying to change views after button is pressed

I'm trying to execute code when a button is pressed for an application but I can't find how to change the views after the code is executed. Is there a way to switch views how I want to or is there another way? Thank you in advanced, I'm very new to xcode.
edit: I'm trying to go from one view to another, not the view controller and yes I have one storyboard that I planned on using for the whole project if possible.
To execute code when a button is pressed, you have to set up a method that the button is hooked into. Because you said you're using storyboards, I'll assume your button is on the storyboard. Go to the assistant editor, hold ctrl, and click-and-drag from the button to the view controller's .m file (#implementation section). This will create an IBAction method, and any code in this method will execute whenever you press the button.
The method will look like this:
- (IBAction)aButtonPress:(id)sender {
}
According to your comments, you say you only want to change the on-screen view, and not transition from one view to the next.
Views are added and removed with the following methods:
[aSuperview addSubview:aSubview];
[aSubview removeFromSuperview];
I can't really tell you much else with a lot more detail from you... and even though I asked twice in the comments and you said you only want to change the view, not the view controller... I think you probably need to transition to a new view controller. Impossible to know for sure as you've given almost no detail...
But if you want to transition between view controllers (which also transitions views), then ...
Create two view controllers on the storyboard. Hook them together with a segue, and give that segue a name. Now, to perform that segue in code:
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME" sender:self];

iPhone Storyboard, programmatically calling segues, navigation issues

So I have an iPhone app. It has a simple structure, all based on a UINavigationController.
I have a storyboard that has one view, a segue to another view, etc. Now this other view has a UITextView that I do not want to edit on this screen - if the user taps this, I want it instead to fly over to a second screen which basically has the same text view, but this one is full-screen, and the user will edit the text on that screen before returning to the previous screen.
So I capture the textViewShouldBeginEditing method. I previously, in the storyboard editor, manually created a push segue from the previous view controller to this new view controller, and named it so that I can call it by it's identity, which I do with:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// This is called when the user clicks into the textView as if to edit it.
// Instead of editing it, go to this other view here:
[self performSegueWithIdentifier:#"editMemoSegue" sender:self];
// Return NO, as I don't actually want to edit the text on this screen:
return NO;
}
Seems reasonable. And it works. Sorta. It does in fact shoot me over to that other view. That other view's events fire up, I set it's text view to become first responder, I edit the text on that screen. Everyone's happy.
Until I want to use the back button to return to the previous view.
Then I quickly find out - my navigation stack is foobared. Most of the time, I have, for some reason, TWO instances of my new editing controller on the stack, so the first time I hit the back button I get the same stuff over again. Then, oddly, occasionally, it will work as intended, and I will see my previous controller with only one back click.
I started reading the log, and I found this:
2012-12-09 09:41:03.463 APP[8368:c07] nested push animation can result in corrupted navigation bar
2012-12-09 09:41:03.818 APP[8368:c07] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
2012-12-09 09:41:03.819 APP[8368:c07] Unbalanced calls to begin/end appearance transitions for <SecondController: 0x83881d0>.
So obviously, I'm doing something incorrectly here. The question is, what? And how do I do what I want in the way that correctly appeases the tiki gods of the iPhone framework?
Check to see if the textViewShouldBeginEditing is being called twice. I've noticed that these kinds of delegate calls sometimes are.
How is your #"editMemoSegue" being created on the storyboard? is it created from the textView? if it is then you should recreate it directly from the view controller or from the top status bar on the view controller that way it wont be called twice when you touch the trigger object and when you call it programmatically.

Adding and controlling view controllers in storyboard in xcode

I am new to iphone programming and I have created a special scenario. I am pretty sure that it will answer most of my questions for creating my app. Here it is. (i HAVE Xcode 4.2)
I have created a universal application with storyboard and single view.
I have appDelegate, storyboard files and a class file ViewController.h/m for my initial ViewController.
Suppose I have added a progress View (Graphically) on the view Controller. Also I have added another viewController on storyboard and made its background black.
When i run the app my first viewController with progress view shows up
Now my questions are
1- How can I link my progress view in the class file and how can i set it progress for 5 seconds.
2- After showing progress within 5 seconds it should switch to other view Controller with black background.
3- I have created a class file "MySecondController" How can i link this class to my black screen viewController.
These are easy questions. I hope I get answer to these. If anyone have tutorials linking to these questions do post. I have tried and haven't found useful.
Best Regards
1a) On the upper right of the xcode window, there's a tuxedo icon. This is the assistant editor. Select the storyboard, select the ViewController, then select the assistant editor. A second editor will appear. Make sure it's editing ViewController.h. Press control and drag from the progress view in the storyboard to a point inside the ViewController interface definition. (right after the line that looks like this: #interface ViewController : UIViewController) You'll be prompted to type an outlet name. Type something like "progressView" (no quotes).
1b) See below to set the progress view to a value.
2) See below to present the second view controller after a delay.
3) In the storyboard, drag a new viewcontroller onto the canvas. On the identity inspector (try the icons under the tuxedo icon, the third from the left is identity), set it's class to MySecondController. Select the original ViewController, press control and drag from it's status bar (the top of it) to the viewcontroller you just added. This will create a segue. A menu will appear asking what kind of segue you want. Make the segue modal for now. Click on the new segue to select it, then click on the attributes inspector (right next to identity inspector) give the segue an identifier, call it "MySecondControllerSegue".
Back to questions 1b and 2) Go to ViewController.m and add this code...
#synthesize progressView;
// this creates methods on self to access the progress view
// this is called after the view appears
- (void)viewDidAppear:(BOOL)animated {
// do inherited behavior
[super viewDidAppear:animated];
// set the progress view value to a number between 0.0-1.0, representing percentage
// notice how we used the synthesized getter: self.progressView
self.progressView.progress = 0.5;
// call another method with no parameters after five seconds
[self performSelector:#selector(doSegue) withObject:nil afterDelay:5.0];
}
- (void)doSegue {
// run the segue that you created in IB
[self performSegueWithIdentifier:#"MySecondControllerSegue" sender:self];
}
Build and run.
After this is working, you'll probably want to show that progress view advancing from the 0.0 position to 1.0 over the course of five seconds. For that, you'll need to read about NSTimer. You can set one up to send you a message periodically, and you can adjust the progressView each time.

iOS Dismis stack of ViewController

I'm developing a iPhone App, in which I'm changing the ViewController using presentModalViewController method upto 2/3 levels (e.g. Home-->Option-->Other-->More).
Now I want to get my app back to first screen or ViewController. If I use dismissModalViewControllerAnimated method, it just dismisses the current view controller only. But I want to clear the stack and start the Home screen again.
Thanks for any help.
Set the delegates properly for each level. So if you press Cancel or Done in More, it will call somehting like [other moreViewDidCancel] inside of which, you will call dismissModalViewController:Animated: and notify its parent view controller (delegate) that is should dismiss( so, [option otherViewDidCancel]) and so on till the top level.

How to go to Previous View? - iPhone Devlopment

I am Using a Navigation based application.
However i don't want to push a view.
I have changed present ModalViewController.
Now, I am confused - how to load previouse view.
My Application's Table View - default in navigation base application
On navigation controller I have added (add employee) button.
On Add(employee) click i have written following code in appDelegate.m
-(IBAction)AddClicked:(id)sender
{
if(self.addData==nil) // addData is a object of my next view
{
addData=[[AddEmpScrn alloc] initWithNibName:#"AddEmpScrn" bundle:nil] autorelease];
}
[self.navigationController presentModalViewController:self.addData animated:YES];
}
This code works successfully & next Add Employee screen load perfactly.
But Now I am confused.
How to get back to previous view?
that is if a user click on "Save" or "Cancel"
it should display again the previous view...
main screen
I dont know how to solve it...
anybody please help me...
i will be thank full.
Have u tried using the dismissModalViewController method? should do what you are asking f or
Have you read "Using Modal View Controllers"?
If you presentModalViewController then the old view is still there "behind" it. You just need to call dismissModalViewControllerAnimated: on the original navigation controller.
If you are directly manipulating the view hierarchy by swapping out subviews, you will need to hold onto the old view in another member variable and make sure to retain it.