Foundational Flashcards
how do we grow app to handle multiple MVC?
we use storyboards and we use “controllers of controllers”, for example, a UINavigationController.
how does “controllers of controllers” structure the multiple MVCs?
it points to a root view controller, it embeds an MVCs View inside its View, then a UI element in this View can segue to another MVC and its View is now embedded in the “controller of controllers” View
how do you “conditionally” segue?
call perform segue with identifier
when a segue happens, what goes on in my code?
source VC offers a chance to “prepare” the destination VC, before it comes on
how do we think of the new MVC we segue to?
we think of it as part of the “View” of the source VC: it communicates through delegation only in this case since target/action is not applicable here.
What goes on in your Controller when the device is rotated?
You can control whether the user interface rotates along with it using (BOOL) should autorotate to interface orientation
If you support an orientation, what will happen when rotated?
The frame of all subviews in your Controller’s View will be adjusted. The adjustment is based on their “struts and springs” you set in the size inspector in XCode. When a view’s bounds changes because its frame is altered, does drawRect: get called again? not by default, but you can change it so that it does redraw
When a view’s bounds changes because its frame is altered, does drawRect: get called again?
not by default, but you can change it so that it does redraw
What will happen when for a subview with “struts fixed to all four sides and both inner springs allow expansion”?
It grows and shrinks as its superview
’s bounds grow and shrink
What will happen when for a subview with “struts fixed to top, left, right sides (not bottom) and only the horizontal inner springs allow expansion (not vertical)”?
Grows and shrinks only horizontally as its superview grow and shrink and sticks to the top in its superview.
Redraw on bounds change?
By default, when your UIView’s bounds change, no redraw Instead, the “bits” of your view will be stretched or squished or moved. Often this is not what you want …
How do you change the default, meaning, when your UIView’s bounds change, how to you set redraw?
Use the UIView @property (nonatomic) UIViewContentMode contentMode; Assign value UIView Content Mode Redraw.
When is initWithFrame called?
When you instantiate with alloc] initWithFrame…
How do you override initWithFrame: ?
self = [super initWithFrame: aRect]
Why use awakeFromNib method ?
Because initWithFrame is NOT called for a UIView coming out of a storyboard, but awakeFromNib is. So you want to put set up stuff in awake from Nib.
Typical initFromFrame: code ?
self = [super initWithFrame:aRect];
[self setUp];
return self;
What are protocols?
Protocols are similar to @interface, but someone else does the implementing.
Where are protocols defined?
In its own header file, or in the header file of the class that wants other classes to implement it.
What does this mean? @protocol Foo
Implementors must implement Foo being declared here, as well as Other.
What does this mean: @interface MyClass : NSObject
MyClass is a kind of NSObject that implements the Foo protocol
id obj = [[MyClass alloc] init];
Declaring an id variable with a protocol requirement
What is the #1 use of protocols in iOS?
delegates and data source
@property (nonatomic, weak) id delegate;
@property (nonatomic, weak) id dataSource;
data source is just like delegate except it’s for delegating data
Why do Views commonly have a dataSource delegating provisions of data?
Because Views cannot own their own data.
How is protocol like static typing?
They’re both compiler-helping-you stuff. They make no difference at run-time. They’re documentation for you method interfaces as well.
What are some powerful ways to leverage the id type?
Use id in protocol
Is the class “UIGestureRecognizer” an abstract class? How do we use it?
Yes, it is an abstract class that we don’t instantiate from. We use the “concrete subclasses” of it like swipe, pinch, tap, pan.
What are the 2-sides to using a gesture recognizer?
- Adding a gesture recognizer to a UIView to ask it to recognize that gesture.
- Providing the implementation of a method to “handle” that gesture when it happens.
What is “panning”?
moving something around with your finger
Who handles recognized gestures?
The View would generally handle gestures to modify how the View is drawn.
The Controller would have to handle gestures that modified the Model.
In this code: UIPanGestureRecognizer *pangr =
[[UIPanGestureRecognizer alloc] initWithTarget:pannableView action:@selector(pan:)];
Is there an argument to pan: ? What is the argument sent?
This version of the action message takes one argument (which is the UIGestureRecognizer that sends the action),
but there is another version that takes no arguments if you’d prefer.
How does the state machine work inside gesture recognizers?
Gesture Recognizers sit around in the state Possible until they start to be recognized. Then they either go to Recognized (for discrete gestures like a tap). Or they go to Began (for continuous gestures like a pan). At any time, the state can change to Failed (so watch out for that). If the gesture is continuous, it’ll move on to the Changed and eventually the Ended state Continuous can also go to Cancelled state (if the recognizer realizes it’s not this gesture after all).
The base class, UIGestureRecognizer provides this @property:
@property (readonly) UIGestureRecognizerState state;
- (void)pan:(UIPanGestureRecognizer *)recognizer
{
if ((recognizer.state == UIGestureRecognizerStateChanged) ||
(recognizer.state == UIGestureRecognizerStateEnded)) {
We’re going to update our view every time the touch moves (and when the touch ends). This is “smooth panning.”
the pan gesture offers a translationInView method.
CGPoint translation = [recognizer translationInView:self];
What is translation? what is the view it refers to?
Translation is the cumulative distance this gesture has moved. Translation gives you a point that tells you the distance of the pan from the last point that was set in the gesture.
Specify which view’s coordinate system you want to be in with transitionInView:self, means this view’s coordinate system.
How to reset the cumulative distance or translation back to zero? And why?
Reset by setTranslation:CGPointZero inView:self
Now each time this is called, we’ll get the “incremental” movement of the gesture (which is what we want). If we wanted the “cumulative” movement of the gesture, we would not include this line of code.
Is Rotation gesture recognizer’s CGFloat rotation property in radians or degrees?
Radians
Is the Pinch gesture recognizer’s scale property read only?
No, you can reset each movement. Reset scale to 1 for the “incremental” effect of this gesture.
What is a view (i.e. a UIView subclass)?
A view (i.e. UIView subclass) represents a rectangular area
Defines a coordinate space
What can you do with a view?
You can use it to draw and handle events in that rectangle
How are views structured/related?
Hierarchical
A view has only one superview - (UIView *)superview
But can have many (or zero) subviews - (NSArray *)subviews
Subview order (in subviews array) matters: those later in the array are on top of those earlier.
The hierarchy is most often constructed in Xcode graphically Even custom views are added to the view hierarchy using Xcode
What about UIWindow?
UIWindow
The UIView at the top of the view hierarchy
Only have one UIWindow (generally) in an iOS application It’s all about views, not windows
Do we always use CGFloat for graphics in views?
Yes.
Just a floating point number, but we always use it for graphics.
What is a CGPoint?
C struct with two CGFloats in it: x and y.
CGPoint p = CGPointMake(34.5, 22.0); p.x+=20; //move right by 20 points
What is a CGSize?
C struct with two CGFloats in it: width and height. CGSize s = CGSizeMake(100.0, 200.0); s.height+=50;
What is a CGRect?
CGRect
C struct with a CGPoint origin and a CGSize size.
CGRect aRect = CGRectMake(45.0, 75.5, 300, 500);