This comes up on Stack Overflow often enough that it seems worth a little article here. The issue is this: You set the frame
of a view or layer in code, but it doesn’t seem to come out in the right place. The reason is usually that you’re doing this too soon.
The dreaded double-segue mistake
This turns out to be a surprisingly common mistake with table views and collection views:
-
In the storyboard, you connect a segue from the cell prototype to another view controller.
-
In code, you implement the delegate method
didSelectRowAt
ordidSelectItemAt
to callperformSegue
to trigger the segue.
The result of this mistake is that when the user taps the cell, the segue is triggered twice.
Don’t touch my outlets
Outlets (@IBOutlet
) are view controller properties that point to views in the storyboard, such as UILabels, UITextFields, and so forth. Outlets are wonderful things, but you should consider them private. Only the view controller that declares an outlet should touch the outlet. Everyone else: hands off.
Don’t make a new view controller by mistake, part 2
As I said in my previous post, there’s a common mistake where a programmer creates a new view controller instance when that isn’t what’s really intended.
Continue readingDon’t make a new view controller by mistake!
One of the most common mistakes I see iOS programming beginners make on Stack Overflow is creating an instance when they intend to refer to an instance that already exists.
Continue readingReturning a value from asynchronous code
By now, you’ve probably read the posts about what “asynchronous” means and how to use a value set by asynchronous code. If you haven’t, please read them now!
So you understand why you can’t use a value set by asynchronous code. Well, that means you can’t return a value from asynchronous code either!
Continue readingUsing a value after it has been set by asynchronous code
By now you may have read this post explaining what “asynchronous” means. If you haven’t, please read it first!
So, if you understand what “asynchronous” means, then you won’t make the biggest mistake that people tend to make with asynchronous code, namely: outside asynchronous code, trying to use a value that depends on asynchronous code.
What “asynchronous” means
Probably the most commonly repeated iOS programming question I see on Stack Overflow is some variation on the problem of what it means for code to be asynchronous. The issue is often exacerbated when the questioner doesn’t even know that a piece of code is asynchronous.