iOS: Segues vs IBAction? - iphone

I am just starting to learn about iPhone development and there seems to be very little resources on using Story Boards.
I have two view controllers, ActionViewController and ActionDetailViewController. AVC list the actions that a user has committed whereas ADVC shows the details of that particular action.
AVC has a "+" button to add a new Action, while AVDC has a "Done" and "Cancel" button.
I really like using storyboards because they are a fantastic way to visualize the app. However, I find that using segues to transition between the two screens quickly becomes visually complicated.
Can someone enlighten me what's the "typical" way of doing this? Do I really have a new segue for each action?
I also tried using IBActions. However, at AVDC I am unable to transit back to AVC with
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Any ideas/

What I would do is set up a single Segue going from the AVC ViewController to the ADVC. Then when you want to perform the segue call
[self performSegueWithIdentifier:#"nameOfSegueIdentifier" sender:self];
Then override the delegate method below to set up the ADVC
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Note you can use
[segue destinationViewController]
to get a pointer to the ADVC so you can manipulate it before it is pushed to the screen.
Hope this helps

Related

Xcode sub navigation views

Does anyone know of any good tutorials for the following please?
Im new to Xcode and dont know where to start with this.
I have a ViewController that is the root View and has 6 navigation buttons (UIButton) on it. Depending on which button that is clicked, the user will see a sub-navigation View of that section with further button options on it.
So e.g top level will have buttons Where to Eat, What to Do...
Then clicking on Where to Eat will show Restaurants, Fast Food ....etc
I would like to do this programatically. I can do it using Storyboards and using multiple views, but it gets very messy as there are a lot of views on the screen eventually.
I have followed a tutorial HERE on how it is done for TableViewControllers, but I need something similar for buttons.
Im not sure what this function is called - have been searching for sub-navigation for the last while but nothing matches what I need to accomplish this.
Check out UIViewController's method presentViewController:animated:completion: method. It is available in iOS 5.0 and up. Let's say you have one of the button's linked to run the buttonOneActivated: method:
-(IBAction)buttonOneActivated:(id)sender
{
UISubViewController *subViewController = [[UISubViewController alloc] init];
[self presentViewController:subViewController
animated:YES
completion:NULL];
}
And in UISubViewController's implementation, let's say you have another button in order to return to the parent:
-(IBAction)returnToParent:(id)sender
{
[[self presentingViewController] dismissViewControllerAnimated:YES
completion:NULL];
}

Custom popups in iOS using storyboard views or separate nibs

What is the most correct/efficient/up-to-date way to present a popup window in iOS?
A la TweetBot:
Methods I've used:
Presented a popup from a separate nib and used loadNibNamed
Presented a popup from the main storyboard with instantiateViewControllerWithIdentifier
Created/Presented a popup purely programmatically*
*I'd rather not use this method because my popup has a fairly complicated custom UI
Also once I get the scene loaded from any of the methods above I don't know how to connect the UI elements in the popup with outlets/actions (Protocols? Delegates?)
If you could point me in the right direction for the most correct way of creating/showing/using a popup that would be fantastic.
Let me know if i'm being too vague, I can add more detail.
EDITED:
Hm, Just saw your screenshot after entering my answer. Well to do that, you can use a the feature provided by UIAlertView. I am pretty sure that is how the TweetBot guys do it as well. This link here shows a blog article that will let you do what you need.
Unless you want to go crazy custom, there are couple of solution out there that let you accomplish it. Something like this
DONE EDIT
One thing you can use is the Segues. Assuming you are using StoryBoard and when you tap on a button and that brings in a pop up view, you can connect them through something like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"ViewToBringIn"]) {
ViewController *vc = [segue destinationViewController];
vc.navigationItem.title = #"some title";
}
}
Another way of using Segues could be through the StoryBoard UI. Click on the view you want, and that will bring up something like the screenshot in the Connection Inspector:
However, if you are using a nib file, then you can still create them and push them into the view (according to the format you want) via code. Of course the design on the view is done through the Interface Builder.

Using segues and nsnotificationcenter

I have 2 viewcontrollers and I want to send a notification out from one to the other and have a label changed based on the name of the notification (pressing a UIButton). I just started using segues and found they are a very useful way to get from one view to another. The problem I am facing is that using a segue (modal at the moment), the second view controller is not receiving the notification. I believe that segues release and create new view controllers when used, not sure. Is there any way around this?
Thanks!
I think using NSNotification is not the right method for passing data from one VC to another. Like Rog said, use prepareForSegue and use a property on the destination VC:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Always check to make sure you are handling the right Segue
if ([segue.identifier isEqualToString:#"mySegue"]) {
MyOtherViewController *movc = segue.destinationViewController;
movc.myProperty = #"MyValue";
}
}
Use prepareForSegue for going-to a segue, and use the delegate pattern for coming back. Within prepareForSegue, set the originating viewController as the delegate of the destination viewController. When returning, have the destination viewController call it's delegate (the original viewController) for whatever method you implement.

Xcode : Storyboard and retaining data in each controller

As you can guess i am a new programmer and i have trouble getting a simple thing!
I am making an app with multiple view controllers. Each controller have textfields and UIsegmentedControl items. When i am moving from one view controller to the other (uding modal trantition if that matters), the contents of the previous one (textfield entries and segmented control option) reset to their original state. How can i make them keep their previous state?
Thanks in advance.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
bViewController *deneme = [segue destinationViewController];
[deneme setPassedValue:label.text];
}
This piece of code will solve your problem, I hope. It saves the label of whatever is inside of it. And you need to add some more code to other classes.
If this code helps you tell me and I can give you the whole code.
To save the application state you can use a model class, following the recommended MVC (model-view-controller) paradigm.
More information here: Retain view state upon reloading
As an alternative you could use the viewWillDisappear: event to save your view state, and then restore it on the viewWillAppear: event.
The viewWillDisappear: event is fired right before the view is going to disappear, and viewWillAppear: is fired before the view is put on the foreground, being ideal to make any changes to the UI.
These events might have already been declared for you in your view controller, but in case they're not check the prototypes here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
You can also use a navigation controller to move from one view to another.
This way, you will push your new view on top of the previous one, and when you go back, the previous view has kept its state.
see this tutorial for more information on storyboard and UINavigationController :
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

iPhone: How to show pop-up windows and dismiss them

In my Universal App I have a long UITableView with custom cells..and for some cells I may need to show some long pop-up explanaiton about that cell when for instance user clicks a "i" label on the cell. In iPad popover view seems excellent choice for this, but don't know how can I implement this on iPhone, what are the possibilities? Also I want to spend as less time as possible when making it work for iPad- popover view. I want to re-use some of the code or logic i use on iPhone
Things came up to my mind;
-Show explaination in alert shild, but the current look and feel of alert shield is ugly can I customize it however I like and show wherever I line on screen and if I can make it scrollable;
-Or maybe I can make a uitextview to show on top, but then how will I dismiss it, I will need some buttons there..which sounds tricky.
-UIActionsheet with a uitextview on it, is reasonable here?
Also I found this code in S.O but dont know how to use this in my case;
newView.frame = CGRectMake(60, 140, 200, 200);
[parentView addSubview:newView];
Have a look at http://iosdevelopertips.com/open-source/ios-open-source-popover-api-for-iphone-wepopover.html. It's a Popover component for iPhone. I think it works best in your case. You can Google "iphone popover" for more options.
We built an open source library for iPad-like popovers on iPhone allowing you to customise the look and feel of the popovers and place any view or controller inside it.
Watch the project on Github and download it at http://www.50pixels.com/blog/labs/open-library-fppopover-ipad-like-popovers-for-iphone/
On dismissing it, see the following instructions:
Know when a new popover is displayed
- (void)presentedNewPopoverController:(FPPopoverController *)newPopoverController
shouldDismissVisiblePopover:(FPPopoverController*)visiblePopoverController;
Use this delegate method to know when a new different popover is displayed. If you want to dismiss the old popover, and release it, send the dismiss message inside this method.
- (void)presentedNewPopoverController:(FPPopoverController *)newPopoverController
shouldDismissVisiblePopover:(FPPopoverController*)visiblePopoverController
{
[visiblePopoverController dismissPopoverAnimated:YES];
[visiblePopoverController autorelease];
}
Know when the popover is dismissed
- (void)popoverControllerDidDismissPopover:(FPPopoverController *)popoverController;
Use this delegate method to know when the popover is dismissed. This could happen when the user taps outside the popover or when a dismiss message is sent by other actions.
Typically if you used a UIPopover on the iPad you use present a Modal view controller on the iPhone.
So if you create a subclass of UIViewController (e.g. called MyViewController), with the necessary subviews such as a UILabel.
MyViewController *infoViewController = [[MyViewController alloc] init];
//pass data to the new view controller, e.g.
//[infoViewController setInfoText:...];
[self presentModalViewController:infoViewController animated:YES];
[infoViewController release];