I'm brand new to Mac OS X development and to Xcode and the Cocoa framework. I've been working through the "Your First Mac App" tutorial on the Apple's developer website and have run into a snag. I've made it most of the way through but am hung up in the section where it shows you how to use Autosizing on the gui objects. When I click on the slider(or any of the objects) and then select the size inspector I do not have any of the autosize items in the inspector window like it shows in the tutorial. How do I get those options to show up?
Xcode – How to get Autosize feature to show up for objects in Interface Builder
cocoaxcode
Related Solutions
First, placeholder is a better word than proxy here.
Normally when you have an object in a NIB/XIB file it means that loading the NIB file will create that instance. The placeholder objects are objects that will already exist when the NIB file is loaded, and they appear inside of the NIB so that you can make connections between the objects that will be created by loading the NIB and the objects that already exist.
The file's owner, first responder and application are all placeholders.
The file's owner is placeholder for the object that will load the nib. All of the NIB loading methods take an 'owner' parameter. When you make a connection with the File's owner, when it's established at runtime, it will be connected to the owner object passed in to the nib loading method. Many UIKit and AppKit classes invoke the nib loading methods for you. NSApplication
, NSViewController
, NSWindowController
, UIApplication
, and UIViewController
all load NIB files on your behalf. When they do that they pass self as the owner parameter to the nib loading methods. That's why when you use a view controller or a window controller you set the file's owner to your subclass and make most of the connections between your views and the file's owner.
The NSApplication
instance is a simple placeholder for [NSApplication sharedApplication]
. That's a global singleton and the icon in Interface Builder represents that global singleton. Loading the NIB file does not create a second NSApplication
instance. By contrast, when a NIB file contains a window, if you load it a dozen times, you'll have a dozen window instances, but still one NSApplication
instance.
The first responder is unique. Connecting an action to the first responder means that when the action is fired, it should dynamically be sent to the responder chain. The responder chain typically starts with the focused view, and continues up through the view hierarchy and includes some controllers and delegates. Each object in the chain gets a shot at handling the action. Menu items work great with the responder chain. If you had a menu item for "Make Bold" that is supposed to make the currently selected text bold, you might start by hooking that up to an NSApplication
subclass, but then you'd have to know all of the situations that "Make Bold" applies, and how to handle them. A text view and an editable web view would probably need different code to handle "make bold" and bottling this all up in one object would get quite complex and wouldn't be very extensible. Instead you could connect the "Make Bold" menu item's action up to a makeBold:
action on the First Responder. This would mean that when the menu item was selected, the focused object, or one of its parents that responded to makeBold:
, would get the makeBold:
message. Now many classes can implement a makeBold:
method and respond to this menu item when they're in focus.
Important: This check should always be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you'll freeze up your app.
Swift
Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift
Test reachability via closures
let reachability = Reachability()! reachability.whenReachable = { reachability in if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } } reachability.whenUnreachable = { _ in print("Not reachable") } do { try reachability.startNotifier() } catch { print("Unable to start notifier") }
Objective-C
Add
SystemConfiguration
framework to the project but don't worry about including it anywhereAdd Tony Million's version of
Reachability.h
andReachability.m
to the project (found here: https://github.com/tonymillion/Reachability)Update the interface section
#import "Reachability.h" // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end
Then implement this method in the .m file of your view controller which you can call
// Checks if we have an internet connection or not - (void)testInternetConnection { internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); }; // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); }; [internetReachableFoo startNotifier]; }
Important Note: The Reachability
class is one of the most used classes in projects so you might run into naming conflicts with other projects. If this happens, you'll have to rename one of the pairs of Reachability.h
and Reachability.m
files to something else to resolve the issue.
Note: The domain you use doesn't matter. It's just testing for a gateway to any domain.
Related Topic
- Xcode – “Unknown class
in Interface Builder file” error at runtime - Ios – Xcode – How to fix ‘NSUnknownKeyException’, reason: … this class is not key value coding-compliant for the key X” error
- Objective-c – How to disable ARC for a single file in a project
- Xcode – MacOS app does not run on the Xcode simulator
- C++ – Is it possible to generate types with all combinations of template arguments
- Ios – Xcode 10: A valid provisioning profile for this executable was not found
Best Answer
This is a new feature in XCode 4 called Constraints. It's just an other way to define how your user interface should be resized when the window (or parent view) is resized. If you want to use the Autoresizing Mask settings from the tutorial, go to the File Inspector (first tab on the right) and uncheck Use Auto Layout.