Pass button Selected state to another view Controller - objective-c

i have Two View Controllers. 1st controller have 1 button. i use
button.selected = YES;
i'm using If statement to do some custom functions if the button is selected state.
-(IBAction)play:(id)sender;
{
if (button.selected)
{
custom code
}
}
when i use this button in same view controller with play button it works. but i want to move the button to another view controller. but it's not working.
question is how to pass button select value to the other view controller?

I don't know exactly what you're trying to achieve. Do you just dragged a button from one interface builder window to another and wonder why that button doesn't trigger 'play:' in VC 2 or do you want to let a second view controller know about the state of a button that is a subview of view controller one?
1) If you just dragged the button from one IB window to another check the connections again. That's like the most usual problem here.
2) If you just wanna let VC 2 know about the state of the button that's currenty on VC 1 one way would be adding a BOOL to VC 2 that represents that button's state. Whenever the button get's triggered it is responsible for updating that BOOL.

Related

Crash when calling performSegueWithIdentifier with verified segue identifier string

I added a "Done" button to my navigation bar programmatically since I couldn't do it on the storyboard.
Next, I wanted to link from this "Done" button to another subclass of UICollectionViewController (both destination and source view controllers are subclasses of UICollectionViewController).
Since, my "Done" button couldn't show up on the storyboard (because added programmatically), I created a new "Done" button on the storyboard just in the purpose of being able to generate a segue identifier between the two view controllers. I assigned the string "donePressed" to the segue identifier as you can see here: https://www.dropbox.com/s/8sd936ml1iably3/donePressed_segue_identifier.png
When I run my build, it succeeds but when I tap on the "Done" button in my navigation bar, I get the following error message: https://www.dropbox.com/s/r5fdg9xs886hxod/Error_Message_performSegueWithIdentifier.png
In essence, it is telling me that my source view controller has no segue with the "donePressed" identifier.
This is weird to me since I purposely created the segue and when I double check my code calling the "performSegueWithIdentifier" method, it looks like I have entered the right "donePressed" NSString.
See here this line of code:
-(void)donePressed {
//segue to other VC
[self performSegueWithIdentifier:#"donePressed" sender:self.doneButton];
}
What am I doing wrong?
Any help much appreciated.
Merci!
The segue you have created looks like it is from your "Done" button to the next ViewController, not from self (primary view controller) to your next view controller. If you have linked the "Done" button to perform the segue all in storyboard, you should not need to write any programmatic code to perform the segue.

popping ViewController into NavigationController

I have a navigation controller, root - tabViewController, and i have one more ViewController, it isn't in Navigation controller, there is in ViewController - button.
How can i do on Clicking this button new viewController pops in current Navigation controller?
After a long series of comments ... How you are using this app couldn't be more different than the original question.
You need to replace the PKRevealController front viewController when the button is clicked something like this:
[self.revealController setFrontViewController: newViewController];
I have try to understend your question but i dont know if its that what you need:
Place a button in your firstViewController
move your mouse on that button and hold "ctrl" key
drag to the next view controller and choose push option
That's it!

Two buttons, one click

I have two buttons on top of each other in my nib. I need both of them to be pressed when tapped, but only the top button carries out its function. Is there a way for the top button to tell the bottom button to activate when the top one gets pressed. I do not think I can just merge the buttons into one and use one -(IBAction)buttonName function because one button is bigger than the other so I do not always need both to activate when one of them is pressed.
Thanks!
Is there a way for the top button to tell the bottom button to
activate when the top one gets pressed.
Not really, but you can have the action for the top button call the action for the bottom button.
Here's one way:
- (IBAction)actionTop:(id)sender
{
NSLog(#"The top button was activated.");
[self actionBottom:self];
}
- (IBAction)actionBottom:(id)sender
{
NSLog(#"The bottom button was activated.");
}
Another way would be to use the same action for both, and figure out what to do based on which button triggered the action:
- (IBAction)action:(id)sender
{
// if the top button was tapped, do this part
if (sender == self.topButton) {
NSLog(#"The top button was activated.");
}
// you want the bottom button to be activated no matter which button was tapped, so
// no need to check here...
NSLog(#"The bottom button was activated.");
}
the bottom button is the whole screen which changes what is displayed.
the top button plays a sound, except there are 4 top buttons that
plays different sounds
It seems like an invisible button covering the whole screen might be the wrong way to tackle the problem. You might look into using a gesture recognizer attached to your view to trigger the change. Your button actions could call the same method that the gesture recognizer uses.
Since one button is larger than the other, I assume that you would like the smaller button to "bring down" the larger button with it, including the change in the visual state. In cases like that you could send your target button a "tap" programmatically, like this:
- (IBAction)largeButtonClick:(id)sender {
}
- (IBAction)smallButtonClick:(id)sender {
// Perform the acton specific to only the small button, then call
[largeButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}

Pushing a navigation controller is not supported- performing segues

I created a new navigation controller in my storyboard (not programmatically!) and set it to be "Root View Controller" to a regular UIViewController and added a button in it which says- forward to the next view controller (this second view controller is a view controller which I want that will have a back button to link to the initial view controller).
Now, whenever I try to link the button to the next view controller- "Pushing a navigation controller is not supported".
Help me please, and thanks
EDIT:
I accidentally subclassed UINavigationController, and not UIViewController in my class.
Thank you anyway.
I've tried this and have no problems, its all done in IB with no additional code required ...
Start a new project, "Single View Application" using story boards
Select storyboard and delete the views its produced.
Drag on a new Navigation Controller (it will bring a table view with it)
Delete the table and the table view controller, so you are just left with the Navigation Controller
Drag on a normal view controller
Right Click and drag from the Navigation controller to the new View and choose "Relationship - Root View Controller"
Drag a "Bar Button Item" on to the Navbar which should be visible on the top of your ViewController, you can rename this Forward if you wish.
Now drag on another view controller which is the one your "Forward" button will push in to view.
Right Click and drag from the bar button to the 2nd View Controller, and choose "Push"
Run the project and you will get a Single view with a Navbar and your button, clicking your button will Push the other view and give you a Back Button to return to the first View Controller. I'll try and post a picture of my storyboard if it helps.
Plasma
I had the same trouble. I wanted to have a navigation controller on each storyboard, so that each could run independently, be individually debugged, and so that the look would be right with the navigation bar.
Using other approaches, I found the UINavigationController would be retained from the original storyboard -- which I didn't want -- or I'd get errors.
Using the AppDelegate in your view controller to set the rootViewController worked for me (borrowing segue naming conventions from Segue to another storyboard?):
- (void)showStartupNavigationController {
NSLog(#"-- Loading storyboard --");
//Get the storyboard from the main bundle.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Startup" bundle:nil];
//The navigation controller, not the view controller, is marked as the initial scene.
UINavigationController *theInitialViewController = [storyBoard instantiateInitialViewController];
NSLog(#"-- Loading storyboard -- Nav controller: %#", theInitialViewController);
//Remove the current navigation controller.
[self.navigationController.view removeFromSuperview];
UIWindow *window = [(AppDelegate *)[[UIApplication sharedApplication] delegate] window];
window.rootViewController = theInitialViewController;
To swap views Programatically you would need to select the segue and give it an Identifier like "PushView" then call it like this ....
[self performSegueWithIdentifier:#"PushView" sender:self];
That will programatically do the same as clicking the forward button. I've created you an example project with the code discussed above. Has an -(IBAction) with code in you can use for programatially changing the view.
PushView.zip
I also wanted to do this, present a screen (that had an embedded navigation controller) when the user pushes a button.
At my first attempt, I connected the segue from the button in the fist screen to the Navigation Controller, and the app was crashing with this error "Pushing a navigation controller is not supported".
This is the solution I found:
Select the segue from the button in the first screen to the navigation controller.
If it had an identifier, copy its name. Then delete that segue.
Then create a new segue by CTRL-clicking the button in the first view controller and dragging to the VIEW CONTROLLER YOU WANT TO PRESENT (not to the Navigation Controller that is pointing at it), and select Push in the small pop up window.
Then click the icon in the middle of the segue and paste the name you copied in the first step as an identifier for it.
IB is going to give you a warning "Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:." Don't worry, it works perfectly.
If you want to customize the string that is shown as the Back button to return, you can add this line in the viewDidLoad method OF THE VIEW CONTROLLER THAT IS BEING SHOWED AFTER THE BUTTON IS PRESSED, that is the Child view controller.
(replace "Settings" with the name you need)
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.topItem.title = #"Settings";
...
}

Is there a way to select a segue anchor programmatically?

Suppose I have a Storyboard containing a view that contains a button. When the user presses this button, a popover comes up.
Thus, I need to set an anchor by dragging the segue to the button using Xcode (and then do performSegueWithIdentifier:).
So, my question is: is there a way to set this "anchor" programmatically?
Thank you.
In my case I've added programmatically several UIBarButtonItem.
The problem of only using an invisible view as an archor is that, if like in my case, the size of the UIBarButtonItem is changing it's size, the arrow of the popover doesnt appear centered, and althought it works, looks a bit strange.
How to solve it.
Create a small view in storyboard ( the size doesnt really matter ), make it invisible, and link it.
In my case this is called invisibleViewAsArchor
Connect the UIBarbutton item with the follow action.
-(IBAction) showMyPopover:(id)sender {
if([self.popoverController isPopoverVisible])
{
[self.popoverController dismissPopoverAnimated:YES];
}else{
self.invisibleViewAsArchor.frame = CGRectMake([sender view].frame.origin.x,
[sender view].frame.origin.y-50,
[sender view].frame.size.width,
[sender view].frame.size.height);
[self performSegueWithIdentifier:#"segue_to_something" sender:self];
}
}
as you can see before it shows the popover (with performSegueWithIdentifier), I'm changing the frame
of the Archor with the values from the button that has fired the event.
Hope it helps.
In the storyboard anchor the popover to some arbitrary button. Don't worry too much about which one as it will get overridden in the code.
In the view controller method prepareForSegue, add the code:
let dest = segue.destinationViewController
dest.popoverPresentationController?.barButtonItem = <your bar button here>
or if you want to anchor to a view instead
dest.popoverPresentationController?.barButtonItem = nil
dest.popoverPresentationController?.sourceView = <your view here>
You can't programmatically create segue's as explained here: Creating a segue programmatically, however, you can configure which destination controller you want to display at run-time. This is explained in the apple documentation here: Configuring the Destination Controller When a Segue is Triggered.
Hopefully this helps!
I had the same problem where I was creating a BarButtonItem programmatically. You may also be able to get around it by creating an invisible, disabled button which you can set as the anchor in IB.