Should IBOutlets be strong or weak under ARC?

WARNING, OUTDATED ANSWER: this answer is not up to date as per WWDC 2015, for the correct answer refer to the accepted answer (Daniel Hall) above. This answer will stay for record. Summarized from the developer library: From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should … Read more

@synthesize vs @dynamic, what are the differences?

@synthesize will generate getter and setter methods for your property. @dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass or will be provided at runtime). Uses for @dynamic are e.g. with subclasses of NSManagedObject (CoreData) or when you want to … Read more

How can I create a UIColor from a hex string?

I’ve found the simplest way to do this is with a macro. Just include it in your header and it’s available throughout your project. #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] uicolor macro with hex values Also formatted version of this code: #define UIColorFromRGB(rgbValue) \ … Read more

UITableViewCell, show delete button on swipe

During startup in (-viewDidLoad or in storyboard) do: self.tableView.allowsMultipleSelectionDuringEditing = false Override to support conditional editing of the table view. This only needs to be implemented if you are going to be returning NO for some items. By default, all items are editable. – (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return YES if you want … Read more

Giving UIView rounded corners

Try this #import <QuartzCore/QuartzCore.h> // not necessary for 10 years now 🙂 … view.layer.cornerRadius = 5; view.layer.masksToBounds = true; Note: If you are trying to apply rounded corners to a UIViewController‘s view, it should not be applied in the view controller’s constructor, but rather in -viewDidLoad, after view is actually instantiated.

What does the NS prefix mean?

The original code for the Cocoa frameworks came from the NeXTSTEP libraries Foundation and AppKit (those names are still used by Apple’s Cocoa frameworks), and the NextStep engineers chose to prefix their symbols with NS. Because Objective-C is an extension of C and thus doesn’t have namespaces like in C++, symbols must be prefixed with … Read more

Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy

Where are you calling this method from? I had an issue where I was attempting to present a modal view controller within the viewDidLoad method. The solution for me was to move this call to the viewDidAppear: method. My presumption is that the view controller’s view is not in the window’s view hierarchy at the … Read more

@class vs. #import

If you see this warning: warning: receiver ‘MyCoolClass’ is a forward class and corresponding @interface may not exist you need to #import the file, but you can do that in your implementation file (.m), and use the @class declaration in your header file. @class does not (usually) remove the need to #import files, it just … Read more

Placeholder in UITextView

I made a few minor modifications to bcd’s solution to allow for initialization from a Xib file, text wrapping, and to maintain background color. Hopefully it will save others the trouble. UIPlaceHolderTextView.h: #import <Foundation/Foundation.h> IB_DESIGNABLE @interface UIPlaceHolderTextView : UITextView @property (nonatomic, retain) IBInspectable NSString *placeholder; @property (nonatomic, retain) IBInspectable UIColor *placeholderColor; -(void)textChanged:(NSNotification*)notification; @end UIPlaceHolderTextView.m: #import … Read more

tech