Detecting which button click from previous view controller - ios

I have two view controllers "SortViewController" and "CatalogeViewController".
I have on "SortViewController" 6 Button which connected with push segue to "CatalogeViewController"
how can i know from which one of the button the user came from?
I set an identifier for the segue and I have thought about prepare for segue and sending an string variable to "CatalogeViewController" while the user press on button and to check if it the same string on "CatalogeViewController".
There is another way to do that?
and please some one can explain me how to do preapre for segue with string variable according to my question.
Thanks

Bouabane's answer would work, but it would require creating a different segue for each button, which is a lot more work initially, harder to maintain, and clutters your storyboard with lots of segue connections.
Since all of your buttons link to the same destination, I would do it differently.
Create a segue manually by control-dragging from your first VC (ViewController) to your second VC. Give it a unique identifier like "ButtonSegue"
Then create an IBAction for all of your buttons. Link all of your buttons to the same action.
Give each button a unique tag number. Use a sequential range of numbers (e.g. 101, 102... 106).
In your IBAction method, save the button's tag number to an instance variable.
In your prepareForSegue, pass the tag number instance variable to your destination VC. (The code to match a number is faster and simpler than the code to match strings. With numbers you can use a switch statement.)
To make the code cleaner and easier to read, I would define a typedef'd enum with the possible tag values, then make your source view controller's tag number instance variable and your destination view controller's tag number property use that type.

You should use this method.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
This is an example how you can accomplish your need:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
CatalogeViewController *catalogeViewController = segue.destinationViewController;;
if([segue.identifier isEqualToString:#"firtsSegue"])
{
catalogeViewController.selectedButton = #"firstButton";
}else if([segue.identifier isEqualToString:#"secondSegue"]){
catalogeViewController.selectedButton = #"secondButton";
}else
.
.
.
}

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 segues

I'm creating an app in Xcode that currently consist of a Navigation Controller a Table View Controller and a regular View Controller.
I'm using StoryBoard and have created a segue between the table view and the regular view controller. In the navigation bar I have a button that I've dragged to the view controller in the StoryBoard. When I click at the button, the new View Controller is viewed like it suppose to. I then tried to pass data from the table view controller, see below:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"läggTill"])
{
// Get reference to the destination view controller
AddPlayerViewController *apv = [segue destinationViewController];
[apv.myTextField setText:#"hello"];
}
}
The segue identifier is "läggTill", but the code inside the if-statement is not executing.
Two questions:
What is wrong with this approach?
Is this the best approach when using StoryBoards? Can I pass data via viewWillDisappear?
Can I use segues to pass data back to the table view controller from the view controller?
It makes all the sense in the world to pass your data in the prepareForSegue:sender:. I would not recommend passing data in viewWillDisappear because it just makes it messy and it reduces readability + it becomes harder to keep track.
I think your string comparison is not working! Put an NSLog for your identifier string see on the console. I have a feeling it might not like the "ä" character in comparison.
About your last question, as Gabriel.Massana pointed out, for passing data back, using delegate is the way to go.
Note 1: Another problem I noticed is a possible typo you might have "apv" and "avc".
Note 2: Another reason for it failing is that you are setting the TextField before viewDidLoad gets called on your destination controller. I suggest that pass it as string and in the viewDidLoad of your destination, set the text to your TextField.

Detail view without using a UITableView?

I asked a related question that I realized was way over-complicated, so I made some design decicions and have simplified what I need to do here. I am using Xcode 5 and designing for iOS 7.
I have a navigation controller, a main menu (menuViewController) with a button, and a calculator (calculatorViewController) that is arrived at when the button on the main menu is clicked.
My Xcode storyboard is set up like this and it works perfectly (not an image, but I think this should be clear):
navigation controller -----> menuViewController ------> calculatorViewController
I would like to set up my main menu to have 3 buttons, each going to one of the following:
2 separate independent calculatorViewController units
1 other unrelated view
Because of the additional unrelated view, and because I don't think it makes sense for a few other design and content reasons, I do not want to use a UITable Master / Detail setup.
My question is how I might create a "duplicate" of my view and its code that can link to a second button on the main menu and run independently of the first. I know how to link the button from the main menu to the second instance of the view, but not how to create the second instance in the first place.
Of course, I want to conserve on memory, amount of code, and use best practices.
Any help would be greatly appreciated. Thanks in advance!
EDIT: It looks like this is unclear. I'm trying to figure out how to make it moreso.
Menu Buttons
button 1 ----> goes to calculator 1
button 2 ----> goes to calculator 2 (a second, identical, independent version of calculator 1)
button 3 ----> goes to another unrelated view
I know how to make the buttons get to the views. I am trying to figure out what the best way is to create the second calculator. Also, I want to make it clear that both of these calculators need to be able to run at the same time, and so both need to stay on the stack.
Thanks.
EDIT: Thanks for bearing with me.
Here is my code declaring the properties in my destination VC .h file:
// Properties for segue identifiers
#property(nonatomic, readonly) NSString *tankCalcOne;
#property(nonatomic, readonly) NSString *tankCalcTwo;
Here is the code in the .m file for my menu / source VC:
// This allows the view for tankCalcOne or Two depending on which button is clicked in the menu
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"tankCalcOne"])
{
[[segue destinationViewController] TankCalculatorViewController:self];
}
else ([segue.identifier isEqualToString:#"tankCalcTwo"]);
{
[[segue destinationViewController] TankCalculatorViewController:self];
}
}
I am getting 'No known instance instance method for selector TankCalculatorViewController' errors in both halves of my if/else statement. I can see why, since I haven't declared them, although I am not sure where I should declare them, as TankCalculatorViewController is the name of the view itself.
I don't think you need a second instance. You do not have to use the push segue. If you use the push segue, it will automatically carry the navigationController. Use another segue or a custom one..
I'll try explain how I would do this.
Create all needed segues and views/scenes in your storyboard (only one calculator scene with two segues leading to it)
Set different identifiers to your segues. This is important.
Create source code for your viewcontrollers and assign them to your storyboard's scenes (again only one calculator view controller with a property for you to specify mode)
In your menuViewController setup your - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method. Check segue.identifier in order to know which segue exactly is this (you set those in step 2). There in segue.destinationViewController variable you will get already created your target view controller so you can set calculatorViewController.useSecondVariant or whatever property you choose to use in order for your calculator to know its working mode.

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;
}
}

iOS Button Handler, segue based on button choice

The kind of design that I'm trying to accomplish in my app currently consists of a navigation controller that is the root controller for another View controller known as Test. Test consists of a UITableView in the top half and an UIImageView in the bottom half. THe navigation bar on the top (which is there as a result of the navigation controller) contains two buttons. The image at the link below ( I don't have the reputation points to post an image directly) should make it very clear.
http://i.imgur.com/GM5eH.png?1
I want my design to be in such a way that depending on which button is pressed the image in the imageview is changed while, though the the text in the table view remains the same, they will transition to completely different screens going forward. To give an example, regardless of which button is tapped my table view will consist of : Option 1, Option 2 and Option 3. However, Option 1 for button A is different from Option B and so on.And, this is where the challenge is for me. I have been able to swap out the images based on the pressing of the button succefully. I did this by using an IBActionNavBarButonPressed and then swapping out the images based on the sender tag. Unfortunately, I don't know how to proceed from here. So for example I have my next couple of screens here. But, how do I set up the segues/transitions in such a way that only Option 1 of choice A goes to a certain screen and so on. From my understanding, I'm looking at a combination of prepareforSegue and the navBarbuttonPress IBAction but I'm still not sure how this would work.
Guys, I'm fairly certain as to what I'm trying to but since I'm new to objective C, I'm not completely sure of how to do it. Essentially, I want the logic to be something like, if(element.selected==0) && (IBActionProvider==1) { performSegueWithIdentifier:#"blah" sender.self]; My issue is whether to put this in the IBAction navBarButtonPressedMethod or to put it in the didSelectRowAtIndexPath method.
Thanks and sorry for the long question!
First you give names to other storyboards views. It contains left hand panel(Storyboard Identifier :).
You should create segue for all buttons and give identifier to it.
Then you should use prepareforSegue like this
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ToContacts"])
{
// Get reference to the destination view controller
ContactVC *targetVC = [segue destinationViewController];
// Pass any objects to the view controller here, like...
targetVC.CompanyNme = selectedRowValue;
}
if ([[segue identifier] isEqualToString:#"ViewCompany"])
{
ContactVC *targetVC = [segue destinationViewController];
targetVC.CompanyNme = selectedRowValue;
}
}
#"ToContacts" means your storyboard identifier.

Resources