I have created two views in my main storyboard ->
Now I added a button on the first one which should link to the other one (so when I press it it should lead to the other view). I have id't the one I want to link to with SceneViewController.
I copied the following code (and defined the method in the header) ->
- (IBAction)button:(id)sender {
SceneViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SceneViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
But nothing happens when I press the button. Anybody a clue on what I'm doing wrong?
Thanks, cheers!
It's not enough to just copy the IBAction code. You have to link the button in the storyboard to the IBAction method you created in your header file.
Open your storyboard, click on the Assistant Editor icon at top right of screen (little icon of a waistcoat and bow tie). Make sure the right pane has your header file selected, if not you can chane to it in the drop down at the top.
Then zoom in on your button, right click on it and drag to the IBAction line in your header file. This should create the connection for you.
Related
I created a new Xib file, called MainViewController.xib. I dragged a View onto the builder, then plopped a UIButton on top of the view. Nothing happened, my button is on the view but not considered a subview. There is no hierarchy tree on the left column under the View icon that tells me my button is a subview.
Why is that? Every time I control+drag from the button to the view icon, the information for constraints pops up, instead of anything regarding an IBOutlet connection.
I'm using Xcode 7.1.2
I can control+drag to from my button to my .h file and create a property, but other than that, it's not recognized as a subview. Any help at all will be appreciated, I'm losing my mind.
How do I add code for a button on a ViewController? For example, in VisualStudio, I can simply double-click on a button and it will take me to an auto-genned clicked handler for the button in the codebehind. How do I go about doing something similar to this in xcode?
Steps to generate button code.
1. Create button using drag and drop from the list of components.
Now click on this button(shown in second image )
It will automatically directs you in your code file.
But if some problems occurs to direct you jn your code file.
Follow this steps.
1. Click on your viewController where you added button. (In viewcontroller scene)
2. Click on the identity inspector check the class name.
3.Check same class name is there in the second part.
If its same than now you can simple using control + three finger and drag it to your .swift file it will open it like this.
Now select action in connection to get button click action. And remains outlet to get the button property.
But if you are not getting the same file select here and find your file name in this hierarchy. After getting file do same process again.
Just do what I say it's simple.
1.Open File Navigator and Select your View's viewController.h file.
2.Now left click on your button and click+control drag to viewController.h (make sure you drag between #interface ---- #end).
3.Give name to your method and make sure You select Action in Connection option.
enter image description here
4.Click connect and you will have your method declaration on viewcontroller.h .
5.Now switch to viewController.m and there you'll find your method at the end and there you will write all the defination of your method.
enter image description here
I have a project that requires to load another xib file when a user press a button
In the main page of my project there are 6 buttons
each is link to another xib (NSViewController)
When a user presses on a button it will show a nib file
Here's what I've done
-(IBAction)About:(id)sender {
self.aboutViewController = [[AboutViewController alloc]initWithNibName:#"AboutViewController" bundle:nil];
self.aboutViewController.view.frame = ((NSView*)self.window.contentView).bounds;
}
is this correct?
Please see my comment above but at a pure guess, I don't see [self.window.contentView addSubview:self.aboutViewController.view] being called anywhere, so if your problem is that you're not seeing the view, that's at least one sure reason why.
I can't get the standard back button of iOS into a navigationBar because I can't find it in the Object Library, so can I do it with code or something else?
I just want the normal, standard, blue back button - you know which I mean.
To "automatically" have a back button you need first have a UINavigationController. Then you need to take a different UIViewController and add it as the root view controller in UINavigationController's init method:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:someOtherViewController];
Be sure to also set a title for someOtherViewController, usually in it's viewDidLoad or initializer. I'll tell you why this is important in a second:
self.title = #"Some other VC";
Then take a second UIViewController and push it onto your navigation controller:
[navigationController pushViewController:anotherViewController animated:YES];
You now have two UIViewControllers on your navigation stack: someOtherViewController and anotherViewController.
Your view will now have a back button with "Some other VC" in it. This is the title of the view controller that was just moved out of view:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/
I would also suggest reading up on how UINavigationControllers work and searching this site a bit more for customizing the back button. There are plenty of threads about it.
You can't add the back button yourself. The back button is part of the Navigation controller. If you embed a Navigation controller into your view(s), the back button will appear and be populated by the name of the previous view.
If you're using storyboards select your view controller, then in top menu choose "editor" -> "embed in" -> "navigation controller".
Edit: Here is an exmaple.
I'm running Xcode 7.2. This was driving me crazy, but I figured it out. Here are all the pieces you need to make the Back button appear (make a test project to prove it):
1) You have to have a Navigation Controller and it has to be set to be the initial view controller. So add the Navigation Controller, you will import two tables. Click on the Navigation Controller and on the properties list, check the box that reads "Is Initial View Controller". You will now see and arrow pointing to this view.
2) In our case we want a ViewController and not the included / connected TableViewController, so delete the TableViewController (RootController) and add a new ViewController.
3) Connect the Navigation Controller to the new ViewController by clicking on the top bar of the Navigation controller and orange circle with the arrow pointing left. Hold the Control button on your keyboard down and click and drag from the orange circle to the ViewController and let go. When given the list of options on how to connect the two views, select 'root view controller'.
Done! Now you the functioning navigation bar and you automatically get the back arrow on all segues added. Test this. Add another ViewController and connect to it with a button on the existing ViewController. Use the Control-click-drag approach from the button to the newest ViewController. Select the 'show' option for the new segue you created.
Run it. You'll see the back option has automatically appeared when you click the button and moved to the newest ViewController.
This is all provided by the Navigation Controller, but only when you make another controller the RootController. Happy navigating!
Please glance at the image below and help me find a File Owner for the class.
Generally i would connect my UILabel to it, but, alas, i can't find it.
Question: What should i connect my Label to?
Storyboard:
Meanwhile class is set up as
As storyboards don't have an owner, you can use the View Controller instead.
Ctrl click (or right click) the label, drag the blue line to connect up with the orange View Controller.
Right click the Label and connect to the View controller scene
You have put your finger on a key difference between storyboards and nibs: when a nib is loaded, an owner instance is specified, but a storyboard is not loaded with an owner, so there is no file's owner in a storyboard. Your ViewController instance is created by the storyboard and is proxied in the scene (listed as View Controller), so you can draw a connection between that and an interface item. But if you want to form a connection with an already-existing instance not represented in the storyboard, you'll have to identify that instance in some other way (perhaps by a tag) and find it and runtime and form the connection in code after the storyboard loads.
For example, in this code, I manually load a storyboard (to use its initial scene in a popover) and then form connections from some bar button items within it:
UINavigationController* nav =
(UINavigationController*)[[UIStoryboard storyboardWithName:#"Storyboard"
bundle:nil]
instantiateInitialViewController];
// there is no file's owner...
// so we can't just draw the connection from button items to ourself,
// because we are not proxied in the storyboard
// so, locate the button items in some other way and do it in code
UIViewController* root = [nav.viewControllers objectAtIndex: 0];
[root.navigationItem.leftBarButtonItem setTarget:self];
[root.navigationItem.leftBarButtonItem setAction:#selector(save:)];
[root.navigationItem.rightBarButtonItem setTarget:self];
[root.navigationItem.rightBarButtonItem setAction:#selector(cancel:)];
In some cases, there's a trick you can use to inject an arbitrary existing instance into a scene so that a connection to it will work: make that instance the first responder. There is a first responder proxy in every scene, so this can give you something to connect to by drawing within the storyboard. So, this code could work instead of the above:
[self becomeFirstResponder];
UINavigationController* nav =
(UINavigationController*)[[UIStoryboard storyboardWithName:#"Storyboard"
bundle:nil]
instantiateInitialViewController];
(And the button action connections have been drawn in the scene from each button to the first responder proxy object.)
Menu: Navigate - Reveal in Project Navigator
In the Project Navigator, Click on the "Main Storyboard"
Menu: View - Show Assistant Editor
You should have the Storyboard on the left with your label, and the view controler.h text on the right.
Click on your label, hold down the control button, and drag a blue line to the View Controler.h source code on the right. Type in a reference name (for example myLabel), and click connect.
Automagically you will see something like this generated:
#property (weak,nonatomic) IBOutlet UILabel *myLabel;
Inside the View Controler.m, you will see something like this generated:
#synthesize *myLabel;
Inside your IBAction events, you can set the label:
myLabel.text =