One segue - a few buttons - ios

Okey guys, here's the deal...
I'm trying to connect two views with one segue and writing in Label a tag from clicked button. I hope you know what I mean...
Here's the example:
Title view appears and you have to choose the difficulty 1-3 (tag values of buttons) and after clicking your're moved to game view but you'd like to use tag values from title view buttons in game logic.
Here's the sample screen:

This is what I've found and this perfectly suits!
// When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"Segue" sender:sender];
}
// This will get called before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"Segue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}
And here is the link to whole topic.

Set your buttons to trigger the segue that presents the second view controller.
In your first view controller, implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender and use the sender to retrieve the tag.

Related

Programmatically segue..works ever

I'm trying to create a manual segue, which happens only if there is a certain conidition (a successful login).
The steps I have followed are these:
1) I've created a button in source viewcontroller, and i connected the button with a drag with the destination viewcontroller.
2) I set the identifier for the segue, calling it login_ok
3) I've created a -(IBAction)buttonPressed:(id)sender
Now, I tried to make an example, to see if the work or not. I wrote:
- (IBAction)buttonPressed:(id)sender
{
BOOL test = false;
if(test == true)
{
[self performSegueWithIdentifier:#"login_ok" sender:self];
}
Theoretically, the segue should not work, because i set test=false, and i said the segue should work only if test=true.
But the segue works and makes me go to the destination viewcontroller.
Why?
If you connected the button with a drag to the destination controller, it should segue anyway.
You need to connect the segue between screens and not from the button to the screen. Then, you can control your segue from the code.
You should define your segue between controllers not from button to the controller. Defining segue between view controllers is more useful than defining segue from any component to a view controller, because when you want to use a condition for any action, you can use these two methods effectively
[self performSegueWithIdentifier:#"segueID" sender:self];
and
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
For example:
- (IBAction)buttonPressed:(id)sender
{
BOOL test = YES;
if(test == YES)
[self performSegueWithIdentifier:#"login_ok" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"login_ok"])
{
YourNextController *controller = [segue destinationViewController];
controller.welcomeText = #"Hi! Test is true congratulations";
}
}
Especially passing data from controller to another one, this way is always useful.

Need some insight to make a 3 UIButton menu in XCode that toggles and changes dataset in the main ViewController

I'm new on Stackoverflow and I'm currently learning XCode from scratch and I'm in a process of making a Single Page Application with options.
Anyone knows how to efficiently make a simple menu with multiple selectable UIButtons that make the main ViewController display different datasets depending on the selection in XCode?
Tried different things (creating SecondViewController for example but can't figure out how to pass data from it to main ViewController).
Any help will be greatly appreciated.
Refer this:
https://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
How to pass prepareForSegue: an object
Simple Test from above Answer Reference:
Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
REVISION: You can also use performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.
For instance, consider I had two view controllers. The first contains three buttons and the second needs to know which of those buttons has been pressed before the transition. You could wire the buttons up to an IBAction in your code which uses performSegueWithIdentifier: method, like this...
//When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}
Hope this help but please go through refernces!

Passing data to destination view controller with performSegue

So I know that if there is a segue which is setup and activated through the storyboard, one can use the prepareForSegue method to pass data to the destination view controller.
I'm wondering if there is a way to do this if one uses the performSegueWithIdentifier method? It seems the prepareForSegue method is only called if the segue is activated with the storyboard, not with performSegue.
One particular problem is that I can't access the UIStoryboardSegue (it is not an argument in performSegue as it is in prepareForSegue), and therefore can't access the destination VC either.
Answer is here in StackOverflow.
How to pass prepareForSegue: an object
// When any of buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}

Navigate to next view via Segue

Upon clicking on the button, I want the user to navigate to another view. The following code works fine. However, I want to pass some values as well to the next screen. How am I suppose to do so ?
- (IBAction)buttonClick:(id)sender {
[self performSegueWithIdentifier:#"segueviewc" sender:self];
}
I also tried the following code and it works but the page navigates twice to the MyV viewcontroller. From buttonClick event and from prepareForSegue method. How can I prevent this ?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MyV *myv = (MyV*) segue.destinationViewController;
myv.navigationItem.title=#"Hey";
myv.arr=data;
}
Open the storyboard check if the segue is directly connect from the button to the next view. If it does, you can simply delete the line.
[self performSegueWithIdentifier:#"segueviewc" sender:self];
i think the problem is, the segue is directly connected from the button action to the next view, and within the button action, you call the segue again!
cheers
You don't have to perform the segue on your button click. If your button is triggering the segue, it will be performed automatically.
Remove the line -
[self performSegueWithIdentifier:#"segueviewc" sender:self];
from your method -
- (IBAction)buttonClick:(id)sender {
Also you must ideally check which segue is being performed, so that you can code according to the navigation being performed...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.idenifier isEqualToString:#"segueviewc"])
{
MyV *myv = (MyV*) segue.destinationViewController;
myv.navigationItem.title=#"Hey";
myv.arr=data;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segueviewc"]) {
MyV *myv = (MyV*)[self.storyboard instantiateViewControllerWithIdentifier:#"yourStoryboardID"];
myv.navigationItem.title=#"Hey";
myv.arr=data;
}
}
And you do not need IBAction method.
There are 2 ways to trigger a segue:
1. You trigger it as an action from your button directly to the new view controller
2. You trigger the segue from one view controller to the second view controller.
I am guessing you are connecting it via 1st approach. Hence you should not trigger it from IBAction.

changing the button image from another .m file

I am trying to make a planning poker app.
I have the following set up
ViewController: where the user selects the cards, has 12 buttons.
a viewcontroller on storyboard: has a big button with the back side of the card
CardViewController: has a button view that should display the selected card dynamically.
Now i am having trouble displaying the right card in CardViewController. I've tried the prepareforsegue function but it's just not working. I also tried using a global variable but my objc skills aren't strong enough.
Can someone help or suggest what i can do? Thanks!
Code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"display"]) {
// Get destination view
CardViewController *vc = [segue destinationViewController];
// Get button tag
NSInteger tagIndex = [(UIButton *)sender tag];
NSLog(#"%d", tagIndex);
// Set the selected button in the new view
[vc setSelectedButton:tagIndex];
}
}
Well now in your CardViewController class you have a method called setSelectedButton you should keep your index to use after that when the view finish loading:
- (void) setSelectedButton:(NSInteger) tagIndex
{
self.selectedTagIndex = tagIndex;
}
- (void) viewDidLoad
{
[super viewDidLoad];
// here use self.selectedTagIndex to do the modifications on the view
}
Hope it helps.

Resources