How do I use PKRevealController with storyboards? - ios

I have been trying to use PKRevealcontroller with storyboards but can't seem to find a way to integrate it. All I want is to have a main view controller and a menu on the left.
I can't seem to figure out how and where to add code. If someone has an example that would be great.
I want to try using PKRevealcontroller because it seems to be the only one that passes information from one view controller to another.

You don't have to use PKReveal if all you want to do is pass data between segues. The best and simplest way to do this would be to use the prepareForeSegue method. The method would look something like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
FirstViewController *transferViewController = segue.destinationViewController;
NSLog(#"prepareForSegue: %#", segue.identifier);
if([segue.identifier isEqualToString:#"joeSegue"])
{
//the data you would pass goes here...
}
}
For a simple explanation, see this tutorial.
If you must use PKReveal, here is a tutorial on how it's done.

Related

Open different ViewControllers from the same TableViewCell (Like facebook notifications)

Here's the issue, I'm trying to make an app that receives updates for different data in the same TableView (like facebook, lets say), that's not the issue however. All data has the same format, the thing is when you click on a row I want to open the corresponding view, I can identify the correct view to display. I try to use the following.
NavigationController.PushViewController (DestinationViewController, false);
When it gets triggered the objects that I created on the storyboard for the DestinationViewController appear to be null, and all the "glue" code is on the ViewDidLoad or ViewWillAppears (which is supposed to have every object instantiated)
I'm using Tab Bar Navigation Model.
PS: If I use the app and enter to my DestinationViewController it works.
So who knows an approach or has an example of something like this, or something that will get me near the place i want to go?
If you use
NavigationController.PushViewController (DestinationViewController, false);
I believe you need to allocate the view controller before you use it with
DestinationVC *vc = [DestinationVC alloc] init];
Correct me if im wrong everyone.
Also if you are using storyboards and have control dragged to different views, you should use the method
[self.navigationController performSegueWithIdentifier:#"" sender:self];
Then you can use the prepareForSegue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"whatYouNamedItInTheStoryboard"]) {
// do stuff for this segue
} else if ([segue identifier] isEqual.......) {
}
}
Hopefully this helps your problem.
You might want to post an image of your storyboard.
I solve the issue kinda like Michael and Paul, says.
Declare a public Attribute on the ViewController.
Then passed the current ViewController to the table source, on the RowSelected Method i put the selected value on the public property of the viewController
And in the PrepareForSegue method i check which type of data it is so i can assign the appropiate segue on the segue.DestinationViewController and pass the data.
I don't know if this is the best approach but, it works good.

How to use Storyboards with multiple segues to one view controller?

My app will use a simple login type selcector view controller, and from that it'll branch out to different view controllers, i.e. Sign Up, Sign In, and Sign In with Social networks. Now how do I add a segue from all of these to one single destination view controller. How do I add these multiple segues.
Every ViewController should have their own seque. To create, press "Ctrl" and move to the ViewController you want
And every ViewController should implement "prepareForSegue"
- (void)prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[MyDestinationVC class]]) {
//do something
//Example
MyDestinationVC *screen = (MyDestinationVC *)segue.destinationViewController;
screen.someValue = self.sendedValue;
}
}
If you have segues between one view(LoginVC) to others (login, social login, etc...)
System already knows last viewcontroller from which you came to newer viewcontroller.
You just execute
[self dismissViewControllerAnimated:YES completion:nil]; or put system barbuttons like UIBarButtonSystemItemDone to take care of dismissal by system itself
to go back.
Unless you have some other problem, this solution should work for you.

How to use segue to pass taken photo

I want to pass a photo taken in the first view controller to a second view controller. I want the user to take the photo in the first view controller and then crop in in the second view controller and then save.. So its like.. User take photo→crop→save. I just want to do this simple task but taking me days to get the segue go right.. Is segue the best way to do this? or is there a more easier way to do this task. I am a beginner in objective -c so its making me confused with segues and all the stuff.
You need to implement prepareForSegue:sender: in the source view controller. This will give you access to the destinationViewController via the passed storyboard. Then you can set the image so its available when the destination view controller is displayed.
As far as segue is concerned you need to create a segue arrow using storyboard by dragging arrow from first view controller to second view controller, and most importantly you need to give the segue an identifier or name.
For firing you segue you may do it programatically, like:
[self performSegueWithIdentifier:#"ShowSecondScreen" sender:self];
For handling things when segue is fired you need to write prepareForSegue() method, you may pass any object from current viewController to next viewController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowSecondScreen"])
{
SecondViewController *secondViewController = segue.destinationViewController;
secondViewController.imageObj = image;
}
}

prepareForSegue **always** creates a new destinationViewController?

I Just realized that the following code always creates a new TagsFeedViewController. Is this the default behavior of segues? Is there a way to configure iOS to not create a new destinationViewController every time?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showSearchResult"]) {
TagsFeedViewController *destViewController = segue.destinationViewController;
destViewController.query = query;
}
}
Segues use whichever view controllers are provided to their – initWithIdentifier:source:destination: methods. It's not the segue that creates the destination view controller, but the storyboard. From the docs:
Normally, view controllers in a storyboard are instantiated and
created automatically in response to actions defined within the
storyboard itself.
So you have some options:
Subclass UIStoryboard. Probably a bad idea. The public interface for UIStoryboard has only three methods; the "actions defined within the storyboard itself" aren't public, and I don't think there's enough information available to let you do the job right.
Make your destination view controller a singleton. Also a bad idea. Aside from the general badness that singletons bring with them, you shouldn't need to keep a view controller that has no views and no child view controllers around. And making your view controller class a singleton just to fool UIStoryboard into using a particular instance of your view controller class seems kinda icky.
Subclass UIStoryboardSegue. If you create your own segues, you can do what you like in – initWithIdentifier:source:destination:, including ignoring the provided destination view controller and using the one you want instead. This still seems like working against the framework, and that's usually a poor plan, but if you absolutely must use a particular instance of your destination view controller this seems like a better way to go.
Go with the flow. Best option. Think about the reason that you're hoping to segue to an existing view controller. Consider whether there might be better ways to accomplish what you want without having to subvert the framework. For example, do you want to use an existing view controller because it already has some particular state? Maybe it'd be better to maintain that state in your model and not in the view controller.
Yes, This is the default behavior for segues. See this post for more information.
You can prevent the creation of the controller by handling the shouldPerformSegueWithIdentifier:sender: message.
-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([identifier isEqualToString:#"showSearchResult"]) {
return [self.results count] > 0;
}
return YES;
}

Drawing in segue vs writing code to transition views - why does data sometimes not transfer?

Let's say I have a UILabel on ViewControllerA and and UITextField on ViewControllerB. I want to go to ViewControllerB and input text then press a button to go back to ViewControllerA. The UILabel should now read whatever was typed in the UITextField.
I was able to accomplish the above by using NSUserDefaults and also using delegation. I am using Storyboards to do this. My question is about the segues used in the storyboards.
It seems when using delegation I must go to and from the storyboard with code and not visually connect the view controllers with a segue in order for the data to transfer. Here is the code when I press a button on my ViewController A:
- (IBAction)pressFirstButton:(id)sender {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
RBViewController2 *vc2 = [sb instantiateViewControllerWithIdentifier:#"ViewController2"];
[vc2 setDelegate:self];
[self presentViewController:vc2 animated:YES completion:NULL];
}
when passing the data back from ViewControllerB to ViewControllerA I do this:
- (IBAction)buttonSegueBackTo1:(id)sender {
NSString *sendThis = self.textFieldVC2.text;
[self.delegate passTextFieldInput:sendThis];
[self dismissViewControllerAnimated:YES completion:NULL];
}
No segue has been drawn between the view controllers and everything works fine. If I don't write this same code, and draw in a segue, the data won't pass backwards. However when I try passing data like this using NSUserDefaults I don't have to write the code to go to and from the view controllers. Instead I can simply connect the view controllers with a drawn segue. The weird thing is, if I'm trying to pass the data in using NSUserDefaults when manually coding the view controllers (an not drawing the segue) the data doesn't transfer.
I'm thinking maybe instead of writing the code in the -(IBAction) pressFirstButton:(id) sender method, I should be putting the code in the prepareForSegue method.
My question is why do drawn segues sometime cause data to be lost? Must all delegation be done without drawn segues? If NSUserDefaults require segues to transfer properly and delegation require code to transfer properly then if I have a view that requires both, it seems that NSUserDefaults will trump the delegation b/c the manual segue being used "resets" the view and only the NSUserDefaults data remains.
Normal segues (any other than unwind segues) ALWAYS create new view controllers. So, if you're using anything other than an unwind segue to go back to A, you're really not "going back", you're creating a new ViewControllerA. Unwind segues aren't normally used in a case like you're presenting, just going back one controller, but you could.
This situation also isn't a good place to use user defaults. The Apple recommended way, is to use delegation, like you do in the code your question. The way you show, is probably the best way to do it, rather than using a segue to go back. You certainly could use a segue to go forward though, and in that case you would implement prepareForSegue: so you can set yourself as the delegate and/or pass any data forward.
Your question is a little dense to parse but I think you are asking if there is a way to get pass data through segues. The answer is yes, and much better than the workarounds you are trying.
In your button you would call:
[self performSegueWithIdentifier:#"scrollerSegue" sender:self];
This will trigger the method below and it is here that you can set the data on the destination viewController. You can set abstract properties on the destination viewController but you can't populate an UIKit elements (labels, imageViews, etc.) because they don't exist yet. Instead set properties and then in viewWillAppear in the destination viewController, do the set up as needed. (alternatively, instead of passing data, you could just set the delegate and then call methods on the delegate to get the data as needed).
For getting data back, using the delegate and calling methods on it seems to be the Apple recommended way of doing things.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"scrollerSegue"])
{
ScrollViewController * target = segue.destinationViewController;
target.assetsArray = self.assetsArray;
target.delegate = self;
}
}

Resources