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.
Related
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 am new to this and I do not understand coding at all, can someone please explain in a very simple way how to fix this?
How do I "go back" from a modal segue that will clear the stack of ViewControllers that are built up from navigating around a sub menu? Currently I have a segue to a sub-menu that has a back button that segue's to the Main Menu. However I ran into memory problems and I need to do it right.
I don't actually have any code in this app, I just have a menu and sub menus that lead to more VC's with images (over 75 images in total). I need the stack of VC's to be cleared from the memory when I go from Sub-menu to Main Menu. I will keep the "back" modal segue from the images to the sub-menu because there are multiple interactions between VC's without going back to the sub menu. So I just need the VC stacks to clear going from the sub menu to main menu. I can link simple code into the back button that is already there but I don't know what I actually need to code to undo the segue and delete the VC's from memory.
Will an unwind segue from sub-menu to main menu delete the VC stack to prevent termination due to memory issues?
Example: http://i.imgur.com/LX8CaFX.png
Edit: I tried using this Dismiss Segue (http://jeffreysambells.com/2014/02/19/dismissing-a-modal-view-using-a-storyboard-segue) however if I switch between image 1 and image 2 then go back to the submenu the Dismiss Segue to the main menu actually sends me back to image 2 and then I get stuck in the sub menu.
I spent hours trying to get this to work and I have no idea, I just want a button that will clear all ViewControllers and get back to main menu.
To clear a view, have an action item (such as a button) on the screen to dismiss. Right-click (or control-click) from the button to the View Controller (the blue thing next to the First Responder button). There will be an IBAction dismissView: (or something similar) under the normal options. That will allow you to close the current view and return to the previous view.
Turns out I'm stupid.
Anyways, it's still a very similar process in iOS. There is an "exit" button on the right of the First Responder…create a subclass of UIViewController (or just do all this in your existing subclass if you already have). Create a new IBAction method in the subclass called return:
ViewController.h
//All of this, if not otherwise noted, is default code upon creating new UIViewController class.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
//Begin "custom" code
-(IBAction)returned:(UIStoryboardSegue *)segue;
//End "custom" code
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
//Begin "custom" code
-(IBAction)returned:(UIStoryboardSegue *)segue {
}
//End "custom" code
#end
You do not need to put anything inside the method. There will be an "unwind" method in the exit button (return:) if you link it now. Bingo!
I am certain there must be a better way to do this, but it works well enough (At least from what I could tell.
(I'm very sorry for telling you the wrong information!)
Edit: It can also be any name; however the argument must be a UIStoryboardSegue pointer. The method must also be in the parent's class. (e.g. First -> Second -> Third means Third exits with secondReturn: and Second exits with firstReturn:; anything else e.g. Third returning with firstReturn: will be nonfunctional in this example)
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.
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!
Seems like this would be a simple thing, but I cannot find any good info on it.
I just want to be able to 'force' the back button to show. I have a main view and when I transfer to another view there is no back button. But then if I transfer to a 3rd view, the back button appears. This seems to always be the case. Only the 2nd transferred to view shows a back button. I need it to show up on all views except the main view.
I dont need to override it, just simply force it to show where it is not showing...
I know this is an old question, but this might help...
...sometimes you need to embed a view inside a Navigation Controller for the back button to show. Simply select your view controller and select Editor -> Embed In -> Navigation Controller.
If the button does not show (usually if you are presenting a modal view rather than push) then you will need to manually make a 'Cancel' button. Create an action for this and in the method put (I call my method switchback):
-(IBAction)switchback:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
Patch's solution (dismissModalViewController)had been unfortunately deprecated and replaced by dismissViewControllerAnimated.
Here is the current one:
-(IBAction)switchback:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}