changing the button image from another .m file - ios

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.

Related

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!

Pass info to viewcontroller on storyboard

I'm new using storyboards and I'm facing this situation: A uinavigationcontroller contains a view controller (root controller) which contains ten buttons linked each one of then through storyboard to the same view controller.
The behavior of the second view controller depends on the button tapped in the first view controller, but how can identify which button is tapped (like tag value) and pass this info to second view controller?
Thank you.
To add on Daniel's answer:
First, add a public property onto your secondVC that is accessible from your first VC:
#interface SecondViewController : UIViewController
#property (nonatomic) int buttonTagClicked;
#end
You need to set tags up on your UIButtons. This is either done in storyboard or programmatically in your code. I would create a generic IBAction that each button is linked to. You can extract the tag off the button through the sender parameter later.
- (IBAction)buttonClicked:(id)sender
{
[self performSegueWithIdentifier:#"pushSegue" sender:sender];
}
That is linked up to
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"pushSegue"]) {
SecondViewController *destinationVC = (SecondViewController *)[segue destinationViewController];
UIButton *selectedButton = (UIButton *)sender;
destinationVC.buttonTagClicked = selectedButton.tag;
}
}
You can set a segueIdentifier for each connection. Then in your ViewController you could trigger an action based on the identifier you set.
e.g.:
If you select your connection in storyboard you can name it:
And in your ViewController you can trigger an action based on the identifier like this:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue1"]) {
UIViewController *destinationVC = [segue destinationViewController];
destinationVC.property = ...;
}
}

Multiple buttons to one Table View

Can someone point me in the right direction? I have one View Controller with multiple buttons. I want the button presses to open my custom one Table View Controller and load the data dynamically. I know I could make multiple Table View Controllers for each button press, but I was thinking there has to be a way to do this dynamically. Let me be clear that another view will determine which data to load into the Table View Controller. I'm new to iOS and Objective-C, but not programming, so take it easy on the rocket science answers. Let me know if you need to see some code.
Basically I have 4 buttons on one View Controller that when pressed will determine what data to load on the Table View. Let's keep it simple for now and just say that the data is 4 NSMutable Arrays.
EDIT:Based on babygau's post this is what I came up with:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"allCattleSegue"]) {
CattleTableViewController *controller = segue.destinationViewController;
controller.cattleArray = [[NSMutableArray alloc] initWithObjects:#"cattle1",#"cattle2",#"cattle3", nil];
}
else if ([segue.identifier isEqualToString:#"bullsSegue"]){
CattleTableViewController *controller = segue.destinationViewController;
controller.cattleArray = [[NSMutableArray alloc] initWithObjects:#"bull1",#"bull2",#"bull3", nil];
}
}
- (IBAction)allCattleTouchUpInside:(UIButton *)sender {
[self performSegueWithIdentifier:#"allCattleSegue" sender:self];
}
- (IBAction)bullsTouchUpInside:(UIButton *)sender {
[self performSegueWithIdentifier:#"bullsSegue" sender:self];
}
this gets the job done but now my navigation doesn't work properly when I press the back button. It gives me a blank screen.
UPDATE: Debugging I see that it is calling the prepareForSegue twice; once with the Controller as the sender and again with the button as the sender. Still attempting to understand how this works and how to resolve this navigation issue.
RESOLVED: Apparently I don't need the actions (TouchUpInside) wired up. Thanks for all of the help.
Hi just create one Viewcontroller and TableViewController. Set the tag fro all button and then create one #property int variable in TableViewController then do like this below..
Create common method to trigger for all button in UIControlTouchUpInside .Then in UITableViewController create int variable like this.
TableViewController.h file you create like this.
#property (nonatomic, assign) int buttonSelected;
ViewController.m create common method like this.
-(IBAction)commonMethod:(id)sender
{
TableViewController *tableVc = [[TableViewController alloc]initWithNibName:#"TableViewController" bundle:nil];
if(button.tag==0)
{
tableVC.buttonSelected = 0;
}
if(button.tag==1)
{
tableVC.buttonSelected = 1;
}
[self.navigationController pushViewController:tableVc animated:YES];
}
TableViewConroller.m file do this
-(void)viewDidLoad
{
[super viewDidLoad];
if(buttonPressed == 0)
{
tableDataSourceArray = ...
}
else if (buttonPressed == 1)
{
tableDataSourceArray = ...
}
}
I hope it helpful for you..
Assume that your table has tableViewData is an array.
In each button action, you use the following pattern code:
[self performSegueWithIdentifier:#"YOUR_IDENTIFIER" target:sender];
Implement the following segue delegate method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
YOURCUSTOMTABLEVIEWCONTROLLER *tableVC= segue.destinationViewController;
tableVC.tableViewData = YOUR_CORRESSPONDING_MUTABLE_ARRAY;
[tableVC.tableView reloadData];
}
That's it, you will have tableView display dynamically based on which button you click.
Notice: you have to create 4 segue between View Controller corresponding to 4 buttons and name the identifier for each segue. e.g #"fromButton1toTableVC", #"fromButton2toTableVC" and so on...
You could pass something like the button label or an identifier telling which mutable array to use.
if your doing the transition in code using a segue in your prepareForSegue method you can pass the value to the destination TableViewController i.e.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITableViewController *controller = segue.destinationViewController;
controller.buttonTitle = sender;
}
Then in the button handler
[self perfomSegue:#"segue" sender:buttonTitle];
assign tag for each button. according to tag you can populate different data in table.simply take only one array.populate data to that array according to tag
if(button.tag==0)
{
//array=#[..];
}
if(button.tag==1)
{
//array=#[..];
}
.
.
.
Now use this array in cellForRowWithIndexPath delegate method

Passing Data from a View Controller to a First Detail Controller and then to a Second Detail Controller

Posting my first question here on stackoverflow! Hope my question is clear, here it is...
I have a Navigation Controller embedded in a First View Controller that has a table view. From the table view I have a segue that pushes to a First Detail Controller. Everything works fine, the segue passes the data to the First Detail Controller, however I would like to push again from a button to a Second Detail Controller.
The layout in the storyboard looks like this Image of storyboard
I set-up a Second Detail Controller and pushed to that from a button on the First Detail Controller.
However I can not figure out how to pass the data I have in an array along to the Second Detail Controller. I added an NSLog line to see what was being passed along and the debug panel is outputing "(null)"
Here is my viewdidload and segue code from the .m First View Controller named ColorBookViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
Color *color1 = [Color new];
color1.name = #"Red";
color1.hexnumber = #"FF0000";
color1.spanishname =#"Rojo";
Color *color2 = [Color new];
color2.name = #"Green";
color2.hexnumber = #"00FF00";
color2.spanishname =#"Verde";
Color *color3 = [Color new];
color3.name = #"Blue";
color3.hexnumber = #"0000FF";
color3.spanishname = #"Azul";
colors = [NSArray arrayWithObjects: color1,color2,color3, nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showColorDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ColorDetailViewController *destViewController = segue.destinationViewController;
destViewController.color = [colors objectAtIndex:indexPath.row];
}
}
Here is the view did load from the .m of the First Detail Controller named ColorDetailController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"This is...%#", color.name);
self.nameLabel.text = color.name;
self.hexnumberLabel.text = color.hexnumber;
}
My .h for the SecondDetailController:
#import <UIKit/UIKit.h>
#import "Color.h"
#interface SecondDetailViewController : UIViewController
#property (nonatomic,weak) IBOutlet UILabel *spanishnameLabel;
#property (nonatomic, strong) Color *color;
#end
Here is the view did load from the .m of the Second Detail Controller named SecondDetailController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = color.name;
NSLog(#"This is...%#", color.spanishname);
self.spanishnameLabel.text = color.spanishname;
}
The app runs fine in the simulator, but I get a blank screen for the Second Detail Controller and I get the "null" result from NSLog(#"This is...%#", color.spanishname);
Any suggestions as to why the segue passes and holds the instance for the first Detail View but not the Second Detail View? Or am I missing something more fundamental here? I tried setting up a second segue on the First Detail Controller and the NSLog produces the correct result (the color name in Spanish), but I am not sure how I would pass this onto the Second Detail Controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showMoreDetail"]) {
NSLog(#"This is... %#",color.spanishname); //works fine
self.spanishnameLabel.text = color.spanishname; //throws error
}
}
Thanks for any help or insight!
Download the complete project here
To pass the data to the second ViewController, just do exactly the same thing as you did in the first ViewController: pass the reference of the Color object in the segue.
In the SecondDetailViewController, you already have a color property. You just have to set this property with the same Color object that was selected in your ColorBookViewController. As you already have this reference in the ColorDetailViewController, just pass it directly to the SecondDetailViewController, via segue.
Inside your ColorDetailViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showMoreDetail"]) {
SecondDetailViewController *destViewController = segue.destinationViewController;
destViewController.color = self.color;
}
}
Now, in the viewDidLoad of the SecondDetailViewController, you would be able to set the labels correctly, like in self.spanishnameLabel.text = color.spanishname;

One segue - a few buttons

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.

Resources