This question already has answers here:
UIView vs UIViewController
(3 answers)
Closed 8 years ago.
I'm using a book on programming games in xcode to teach myself.
I downloaded some of the programs and tried to make my own codes using them, but i can't figure out how to create a mainview.h or mainview.m
Also how do these differ from view controller?
commandn -> Objective-C class -> Next
Class: mainview
Subclass of: UIView
-> Next -> Create
A view is an area where stuff can be draw (programatically using a view controller or visually in Interface Builder).
A view controller is a class that that controls how stuff is drawn in a view, it contains all the code and logic).
Related
This question already has answers here:
locking orientation does not work on ipad ios 8 swift xcode 6.2
(7 answers)
Closed 7 years ago.
^Above does not have answer at all...Having issues disabling auto rotation for specific (not all) View Controllers that are inside a navigation controller. Similar questions do not address the ability to disable autorotation for specific view controllers but rather to disable autorotation in all of the view controllers inside of a navigation controller. My navigation controller contains some VCs that I would like to have autorotation and others that I do not want to autorotate. No existing questions answer this satisfactorily.
I made an example project on how to do this: GitHub repo.
While #Sidetalker's answer is correct I think it lacks a bit of explanation.
Basically you create a Custom Class for your UINavigationController and assign it to UINavigationController in Storyboard. In the custom UINavigationController class you override the shouldAutorotate function and check if the topViewController is ViewController(the class of your UIViewController in Storyboard) of the class on which you want to disable autorotate.
In custom UINavigationController:
override func shouldAutorotate() -> Bool {
if !viewControllers.isEmpty {
// Check if this ViewController is the one you want to disable roration on
if topViewController!.isKindOfClass(ViewController) {
// If true return false to disable it
return false
}
}
// Else normal rotation enabled
return true
}
Hi I've seen this question asked a few times already but with no definite answer yet so I created it for xcode 7 and swift2 (which may have changed things a bit anyway).
I created a project using Xcode 7 and Cocoa OSX Story boards + swift2, so my project started with a NSWindowController that Connects to a NSViewController (as expected!). I added a NSToolbar to my window controller and added a NSButton to the toolbar. I changed my NSViewController to be one of the new NSSplitViewController that links to three NSViewControllers and displays their views horizontally - with vertical dividers - (similar to the layout you see in the photo app or pages in Yosemite +). My final goal will be that the button in My toolbar shows and hides the first split.
It is my understanding is, and I would expect that to achieve this I should create an action in the NSSplitViewController that changes the auto layout constrains more or less in the way they are working it out here: How to do collapse and expand view in mac application?.
And then somehow link this action to the NSButton that is in the Toolbar... which happens to be in the NSWindowController (far up and isolated in the hierarchy)...
I have already gone through other questions about NSToolbar and storyboards and failed to accomplish my goal:
The YouTube video: Cocoa Programming L17 - NSToolbar which is the closest I found to solve the problem, but his method does not work for storyboards, only creating your own xib file.
In this question: How to use NSToolBar in Xcode 6 and Storyboard? One person proposes to make the link using the first reponder and expecting everything to hook up at run-time (which looks a bit dodgy and not the way apple would implement it I think...). A second person suggested to create a view controller variable in the NSWindowController and manipulate its properties from there... but again, a bit dodgy too.
One latest comment I saw in that question which seems the best way to tackle the problem (but still not as good as I guess it could be) is to add a NSObjectController to the dock of each scene and when the scene loads, set the values of the objects to the other secene's controller. Is this really the best way to go ahead? If so, how could I achieve this one?
Apple did mention (again) in WWDC15 that they created storyboards for osx and the split-view controller that owns view-controllers so that you can move your logic and work to the specific view-controller, so I would be expecting to do everything from inside my split-view controller as this is the target that needs to change.
Does anyone know how to achieve this from the view controller itself? I really haven't been able to find a way to connect my ToolBarItem to it.
OK, I've created this question quite a few days ago and no answer so far so I've answer with what I recently did to overcome the problem.
After I created my Xcode project I did this:
Created a subclass MySplitViewController for the NSSplitViewController
Added an IBOutlet for each NSSplitViewItem. For example:
#IBOutlet weak var mySplitViewItem: NSSplitViewItem!
Created a subclass WindowController for the NSWindowController
Added an IBAction in the WindowController class that links to the NSToolbarItem (my button)
Added a property that gets the Window Controller's content as MySplitViewController
var mySplitViewController: MySplitViewController {
return self.window?.contentViewController as! MySplitViewController
}
Now I can access the split view controller's property from the Window Controller in the action I created:
mySplitViewController. mySplitViewItem.collapsed = true
I created some sample code that does this (but using a view controller and changing the text for a label here, just in case someone wants to see a working project with this behaviour. And a blog post about it too :)
One person proposes to make the link using the first reponder and expecting everything to hook up at run-time (which looks a bit dodgy and not the way apple would implement it I think...).
I think this first responder method is actually the proper way.
As an example:
Add something similar to the following, in whichever view controller makes sense.
#IBAction func doSomething(_ sender: AnyObject?) {
print("Do something.")
}
This will magically show up in the first responder:
In your storyboard, right-click the orange "first responder" icon above your window controller, and you should see doSomething in the very long list. You just need to connect that up to your toolbar button.
In the following screen capture, you can see my "Toggle Sidebar" button is connected to the toggleSidebar action in my first responder.
I didn't even have to write this method — it's provided by NSSplitViewController:
#IBAction open func toggleSidebar(_ sender: Any?)
So, I was working this same issue and finding no solution as you experienced. I read your post and was trying to figure how I would implement your solution when it occurred to me to use a notification. In about 30 seconds, I had a perfectly fine working solution:
In your windowController add an IBAction to post a notification like so
-(IBAction)toggleMasterViewClicked:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"TestNotification" object:nil];
}
Hook up that action to your NSToolbarItem, then in the viewController add self as an observer for that notification like so
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(toggleMasterView:) name:#"TestNotification" object:nil];
In your case, selector would be updateMyLabelText
I don't really see any downside here. No reference to other objects needed, no dependancies. Works flawlessly for me
While connectiong IBActions works by using either the First Responder or by adding an "Object" to the scene, then changing its class to the window's view controller class, this doesn't help with IBOutlets and delegates that you'd like to point to the view controller.
Here's a work-around for that:
Add the Toolbar to the View Controller, not to its Window. That way, you can make all the IBOutlet connections in the View Controller Scene easily. I've done that for years and found no issues with it, even when using Tabs.
You'll have to assign the window's toolbar in code, then. E.g. like this:
#interface ViewController ()
#property (weak) IBOutlet NSToolbar *toolbar; // connect this in your storyboard to the Toolbar that you moved to the View Controller Scene
#end
- (void)viewWillAppear {
[super viewWillAppear];
self.view.window.toolbar = self.toolbar;
}
This question already has answers here:
What does the "Couldn't compile connection:" error mean?
(2 answers)
Closed 9 years ago.
I made a Dynamic UITableViewCell with an UImageView and an UILabel.
I linked items to code, build it and it gave me this alarm:
Couldn't compile connection: <IBCocoaTouchOutletConnection:0x7ff657212ee0 <IBProxyObject: 0x7ff6572e5e30> => nationLabel => <IBUILabel: 0x7ff6572e2bd0>>
I've never worked with dynamic cells so probably it's a stupid mistake of mine, but I can understand the reason of it.
How can I solve it? Thank you!
Sounds like you're trying to establish outlets from your prototype cell to your view controller. As mentioned in the comments, that isn't going to work because there are potentially multiple cells and they can't all be connected to the same outlet. So, the first thing you need to do is delete these outlet connections from the storyboard. Once you've done that, the code should compile. From there you have two options (well, I'm sure there are other ways to do this, but these are the normal ones):
In the storyboard, set unique values for the tag property for each element, e.g. make the image 1 and the label 2. In your view controller, anytime you need to access an element, you can do so by calling [self.view viewWithTag:].
Create a custom cell subclass of UITableViewCell, assign it to your prototype, and create outlets between the prototype and the subclass.
It sounds like you've tried (1) and got stuck. If you get stuck, just explain the specific issue you're having and someone will help you work it out.
I've been working through the Stanford CS193p course for iOS 5.0 development (available on iTunes U) and have run into a problem whilst attempting Assignment 3 (pdf).
I am trying to implement a graphing calculator on top of a previous calculator model I have developed. In my storyboard I have added a "graph" button to my calculator which segues to a new view controller. Inside this is a view called 'GraphView'. I need to delegate the data source for this view. I can set it inside my GraphViewController like so:
self.graphView.dataSource = self;
I can also set that in Interface Builder. What I would like to do is set it to my CalculatorViewController, which first sent the segue message, but there is no mention of GraphViewController or GraphView in it's code, so I'm not sure how to accomplish this. Interface Builder would not let me drag the data source out of it's own view's frame either.
Short version: I want to delegate my data source from my Graph View to my CalculatorViewController. See the storyboard here: Storyboard.
Any help would be very greatly appreciated.
You can do this in the prepareForSegue function. You can find a similar example to how this is done in PsychologistViewController.m.
This question already has answers here:
Cannot hide Controls in NSView
(2 answers)
Closed 9 years ago.
I am developing an application in cocoa.I need to hide a progress bar placed in an NSView
and show an NSTextfield in that place .I used the following cod
[progressbar setHidden:TRUE]
[textfield setHidden:FALSE];
But this code snippet is not working. Is that a bug.??I used to hide the same code to hide certain other text fields in the same page .But that controls became hidden.looking for a solution...
Are progressbar and textfield outlets ? If yes, make sure they are correctly connected in your nib. Also make sure that you call setHidden: from the main thread.
And unrelated to your problem, in Objective-C you should use YES instead of TRUE and NO instead of FALSE.