Foundational Flashcards

1
Q

how do we grow app to handle multiple MVC?

A

we use storyboards and we use “controllers of controllers”, for example, a UINavigationController.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how does “controllers of controllers” structure the multiple MVCs?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how do you “conditionally” segue?

A

call perform segue with identifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when a segue happens, what goes on in my code?

A

source VC offers a chance to “prepare” the destination VC, before it comes on

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do we think of the new MVC we segue to?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What goes on in your Controller when the device is rotated?

A

You can control whether the user interface rotates along with it using (BOOL) should autorotate to interface orientation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you support an orientation, what will happen when rotated?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When a view’s bounds changes because its frame is altered, does drawRect: get called again?

A

not by default, but you can change it so that it does redraw

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will happen when for a subview with “struts fixed to all four sides and both inner springs allow expansion”?

A

It grows and shrinks as its superview

’s bounds grow and shrink

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)”?

A

Grows and shrinks only horizontally as its superview grow and shrink and sticks to the top in its superview.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Redraw on bounds change?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you change the default, meaning, when your UIView’s bounds change, how to you set redraw?

A

Use the UIView @property (nonatomic) UIViewContentMode contentMode; Assign value UIView Content Mode Redraw.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When is initWithFrame called?

A

When you instantiate with alloc] initWithFrame…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you override initWithFrame: ?

A

self = [super initWithFrame: aRect]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why use awakeFromNib method ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Typical initFromFrame: code ?

A

self = [super initWithFrame:aRect];
[self setUp];
return self;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are protocols?

A

Protocols are similar to @interface, but someone else does the implementing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Where are protocols defined?

A

In its own header file, or in the header file of the class that wants other classes to implement it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does this mean? @protocol Foo

A

Implementors must implement Foo being declared here, as well as Other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does this mean: @interface MyClass : NSObject

A

MyClass is a kind of NSObject that implements the Foo protocol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

id obj = [[MyClass alloc] init];

A

Declaring an id variable with a protocol requirement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the #1 use of protocols in iOS?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Why do Views commonly have a dataSource delegating provisions of data?

A

Because Views cannot own their own data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How is protocol like static typing?

A

They’re both compiler-helping-you stuff. They make no difference at run-time. They’re documentation for you method interfaces as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are some powerful ways to leverage the id type?

A

Use id in protocol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Is the class “UIGestureRecognizer” an abstract class? How do we use it?

A

Yes, it is an abstract class that we don’t instantiate from. We use the “concrete subclasses” of it like swipe, pinch, tap, pan.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What are the 2-sides to using a gesture recognizer?

A
  1. Adding a gesture recognizer to a UIView to ask it to recognize that gesture.
  2. Providing the implementation of a method to “handle” that gesture when it happens.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is “panning”?

A

moving something around with your finger

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Who handles recognized gestures?

A

The View would generally handle gestures to modify how the View is drawn.
The Controller would have to handle gestures that modified the Model.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

In this code: UIPanGestureRecognizer *pangr =
[[UIPanGestureRecognizer alloc] initWithTarget:pannableView action:@selector(pan:)];

Is there an argument to pan: ? What is the argument sent?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How does the state machine work inside gesture recognizers?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
  • (void)pan:(UIPanGestureRecognizer *)recognizer
    {
    if ((recognizer.state == UIGestureRecognizerStateChanged) ||
    (recognizer.state == UIGestureRecognizerStateEnded)) {
A

We’re going to update our view every time the touch moves (and when the touch ends). This is “smooth panning.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

the pan gesture offers a translationInView method.

CGPoint translation = [recognizer translationInView:self];

What is translation? what is the view it refers to?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How to reset the cumulative distance or translation back to zero? And why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Is Rotation gesture recognizer’s CGFloat rotation property in radians or degrees?

A

Radians

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Is the Pinch gesture recognizer’s scale property read only?

A

No, you can reset each movement. Reset scale to 1 for the “incremental” effect of this gesture.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is a view (i.e. a UIView subclass)?

A

A view (i.e. UIView subclass) represents a rectangular area

Defines a coordinate space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What can you do with a view?

A

You can use it to draw and handle events in that rectangle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

How are views structured/related?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What about UIWindow?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Do we always use CGFloat for graphics in views?

A

Yes.

Just a floating point number, but we always use it for graphics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What is a CGPoint?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What is a CGSize?

A

C struct with two CGFloats in it: width and height. CGSize s = CGSizeMake(100.0, 200.0); s.height+=50;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is a CGRect?

A

CGRect
C struct with a CGPoint origin and a CGSize size.
CGRect aRect = CGRectMake(45.0, 75.5, 300, 500);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

Where is a view’s coordinate system origin?

A

upper left

46
Q

What are the view’s coordinate system units?

A

Units are “points” not pixels. Usually you don’t care about how many pixels per point are on the screen you’re drawing on. Fonts and arcs and such automatically adjust to use higher resolution.
However, if you are drawing something detailed (like a graph, hint, hint), you might want to know. There is a UIView property which will tell you:
@property CGFloat contentScaleFactor; // returns pixels per point on the screen this view is on.

47
Q

Explain the UIView’s property contentScaleFactor

A

The scale factor determines how content in the view is mapped from the logical coordinate space (measured in points) to the device coordinate space (measured in pixels). This value is typically either 1.0 or 2.0. Higher scale factors indicate that each point in the view is represented by more than one pixel in the underlying layer. For example, if the scale factor is 2.0 and the view frame size is 50 x 50 points, the size of the bitmap used to present that content is 100 x 100 pixels.

48
Q

Explain UIView’s property CGRect bounds

A

bounds is your view’s internal drawing space’s origin and size.

The bounds property is what you use inside your view’s own implementation. It is up to your implementation as to how to interpret the meaning of bounds.origin

49
Q

Explain UIView’s property for its superclass ie center and frame

A

center is the center of your view in your superview’s coordinate space. frame is a rectangle in your superview’s coordinate space which entirely contains your view’s bounds.size

You might think frame.size is always equal to bounds.size, but you’d be wrong …
Because views can be rotated (and scaled and translated too).

Views are rarely rotated, but don’t
misuse frame or center by assuming that.

50
Q

When do you use bounds?

A

inside a view’s implementation with respect to its own coordinate system

51
Q

When do you use frame and center?

A

Use frame and center to position the view in the hierarchy

These are used by superviews, never inside your UIView subclass’s implementation.

52
Q

Most often you create views in XCode storyboard, but how would you do it in code?

A

Just use alloc and initWithFrame: (UIView’s designated initializer).

53
Q

How do you use a view to do custom drawing?

A

Drawing is easy … create a UIView subclass & override 1 method - (void)drawRect:(CGRect)aRect;

54
Q

Why you never ever call drawRect ?

A

Instead, let iOS know that your view’s visual is out of date with one of these

UIView methods:

  • (void)setNeedsDisplay;
  • (void)setNeedsDisplayInRect:(CGRect)aRect;

It will then set everything up and call drawRect: for you at an appropriate time

Obviously, the second version will call your drawRect: with only rectangles that need updates.

55
Q

How do I implement drawRect?

A

Use the Core Graphics framework. The API is C (not object-oriented). Always use CGFloat.

Concepts:

  • Get a context to draw into (iOS will prepare one each time your drawRect: is called)
  • Create paths (out of lines, arcs, etc.)
  • Set colors, fonts, textures, linewidths, linecaps, etc.
  • Stroke or fill the above-created paths
56
Q

What is the C function inside your drawRect: method to get the current graphics context that determines where your drawing goes, ie screen, pdf, printer..

A

CGContextRef context = UIGraphicsGetCurrentContext();

57
Q

What are the 3 ways to effect transparency of a view?

A
  1. subview’s list order determines who’s in front
  2. alpha (default drawing is opaque)
  3. hide a view by setting it’s hidden property to YES
58
Q

What do you have to do when using “subroutines” to help draw?

A

What if you wanted to have a utility method that draws something
You don’t want that utility method to mess up the graphics state of the calling method Use push and pop context functions.

59
Q

Can you draw text and draw images?

A

Yes you can or more commonly we use UILabel for text, and UIImageView

60
Q

What is the “push” mode in segue?

A

In this kind of segue, the destination vc is “pushed” onto the screen, while the source vc still exists but slides off. Segue always creates a brand-new vc object, never uses any existing on in the heap.

61
Q

When the pinch first starts, meaning your fingers first set on screen, what is the scale value?

A

The scale starts out as 1, and can be reset to 1 for “incremental” zooming in/out.

62
Q

As the pinch zooms out/in, the scale goes up or down?

A

As it goes out the scale goes up ie 1.07, 1.13. As it moves in, the scale decreases to say 0.9, 0.8.

63
Q

How to you create a custom view in Xcode storyboard?

A

Drag out a generic View object and change it’s identity class to say GraphView, or whatever custom View class you made and want to subclass.

64
Q

What is a UINavigationController’s toolbarItems @property?

A

It is an array of UIBarButtonItems

65
Q

Where is the tool bar? Is it hidden by default?

A

At the bottom of the screen and yes it’s by default toolbarHidden is YES

66
Q

Which view’s tool bar does the navigation controller, aka, the “controller of controller” show?

A

It show the tool bar of the view it’s currently embedding

67
Q

How do you create a UIBarButtonItem?

A

Usually dragged out in Xcode, but can be created with various alloc/init methods.

68
Q

A class is

A

a template for an object

69
Q

An instance is

A

a manifestation of a class

70
Q

A message is

A

something sent to a method to make it act

71
Q

A method is

A

code invoked by a message

72
Q

An instance variable is

A

object-specific storage

73
Q

Superclass/subclass means

A

inheritance

74
Q

A protocol is

A

non-class-specific methods

75
Q

iOS is made up of

A

Cocoa Touch, Media, Core Services, and Core OS

76
Q

Cocoa Touch is

A

Multi-touch, Core Motion, View Hierarchy, Localization, Controls, Alerts, Web View, Map Kit, Image Picker, Camera

77
Q

MVC design means

A

divide your objects into 3 “camps”
model is what you app is (but not how it’s displayed)

controller is the UI logic of *how your model is presented to the user that uses views as minions to do the actual display and only the display

78
Q

It’s all about managing communication between camps.

A

MVC

79
Q

Describe view -> controller communication

A

blind and structured

target/action
when something happens in the UI, the view sends an “action to” the controller (the “target” is on the controller) like a button is pressed

delegate/data source
controller sets itself as a view’s delegate via protocol so that the view uses the controller as a data source or delegate of some action

80
Q

What is the controller’s job?

A

To interpret/format Model data before giving it to the view

81
Q

Can the model talk to the controller?

A

No, model should be UI-independent

82
Q

What if the model has data to update?

A

let model use a “radio-station”-like broadcast mechanism called notification & kvo, and controller can “tune in” to interesting stuff

83
Q

Usually we do not access instance variables directly in Objective-C. What do we use?

A

properties. A “property” is just the combination of a getter method and a setter method in a class.
The getter has the name of the property (e.g. “myValue”)
The setter’s name is “set” plus capitalized property name (e.g. “setMyValue:”)

84
Q

Spaceship.h
#import “Vehicle.h”
@interface Spaceship : Vehicle
@end

A
#import superclass's header file, this is usually #import 
@interface class : superclass
85
Q
Spaceship.m
#import "Spaceship.h"
@interface Spaceship()
@end
@implementation Spaceship
@end
A

import our own header file

@interface class()
private declaration
@end

86
Q
  • (void)orbitPlanet:(Planet *)aPlanet atAltitude:(double)km;
A
  • instance method
    (void) return argument
    orbitPlanet method name
    : takes arguments
    (Planet *) pointer to a Planet object
    aPlanet argument variable name
    atAltitude: takes argument
    (double) primitive floating point number
    km argument variable name
    ; end of method declaration
87
Q

What does nonatomic mean?

A

nonatomic means its setter and getter are not thread-safe. That’s no problem if this is UI code because all UI code happens on the main thread of the application.

88
Q

What does @synthesize do?

A

We almost always use @synthesize to create the implementation of the setter and getter for a @property
. It both creates the setter and getter methods AND
creates an instance variable/creates some storage to hold the value.

Calling getters and setters is such an important task, it has its own syntax: dot notation.

89
Q

What is the common naming convention when you @synthesize and why?

A

_ (underbar) then the name of the property is a common naming convention.

90
Q

All objects are always allocated on the heap. How do we access them?

A

So we always access them through a pointer. Always. @synthesize does NOT create storage for the object this pointer points to.
It just allocates room for the pointer. So you have to allocate and initialize the objects before use. You can do it in the getters.

91
Q

What is the [ ] “square brackets” syntax used for?

A

The “square brackets” syntax is used to send messages.

92
Q

Why properties?

A

Most importantly, it provides safety and subclassability for instance variables.
Also provides “valve” for lazy instantiation, UI updating, consistency checking (e.g. speed < 1), etc.

93
Q

Why do you leave out @synthesize?

A

It is not required to have an instance variable backing up a @property (just skip @synthesize). Some @propertys might be “calculated” (usually readonly) rather than stored.

94
Q

Why @property?

A

Pretty.
Makes access to @propertys stand out from normal method calls.
Synergy with the syntax for C structs (i.e., the contents of C structs are accessed with dots too). Syntactically, C structs look a lot like objects with @propertys.

95
Q

What are the 2 big differences between C struct and objects?

A

With 2 big differences:

  1. we can’t send messages to C structs (obviously, because they have no methods)
  2. C structs are almost never allocated in the heap (i.e. we don’t use pointers to access them)
96
Q

Explain strong/weak of iOS reference counting.

A

strong “keep this in the heap until I don’t point to it anymore” I won’t point to it anymore if I set my pointer to it to nil.
Or if I myself am removed from the heap because no one strongly points to me!

weak “keep this as long as someone else points to it strongly”
If it gets thrown out of the heap, set my pointer to it to nil automatically (if user on iOS 5 only).

97
Q

What is the value of an object pointer that does not point to anything?

A

nil (Thus, instance variables that are pointers to objects start out with the value of nil.)

98
Q

Like “zero” for a primitive type (int, double, etc.) Actually, it’s not “like” zero: it is zero.

A

nil

99
Q

All instance variables start out set to ?

A

zero / or if they are pointers then / nil

100
Q

Can be implicitly tested in an if statement if(obj){} //curly braces will execute if obj points to an object

A

nil

101
Q

Sending messages to nil is (mostly) okay?

A

No code gets executed.
If the method returns a value, it will return zero.
int i = [obj methodWhichReturnsAnInt]; // i will be zero if obj is nil
Be careful if the method returns a C struct. Return value is undefined. CGPointp=[objgetLocation]; //pwillhaveanundefinedvalueifobjisnil

102
Q

BOOL

A

Objective-C’s boolean “type” (actually just a typedef)

103
Q

Why CGPoint doesn’t use * ?

A
CGPoint is a C struct, not a class!
It looks like a class name, but notice no * because C structs are passed by value on the stack, not by reference in the heap.
104
Q

What is the calling syntax for a class method?

A

[Class method]
Ship *ship = [Ship motherShip];
NSString *resultString =
[NSString stringWithFormat:@“%g”, result]; [[ship class] doSomething];

105
Q

Explain self/super when in an instance versus in a class.

A

Instance:
self/super is calling instance self means “my implementation”
super means “my superclass’s implementation”

Class:
self/super is this class
self means “this class’s class methods”
super means “this class’s superclass’s class methods”
106
Q

What are the 3 ways to instantiate?

A

1. Asking other objects to create objects for you
NSString’s - (NSString *)stringByAppendingString:(NSString *)otherString;

  1. Not all objects handed out by other objects are newly created NSArray’s - (id)lastObject;

Unless the method has the word “copy” in it, if the object already exists, you get a pointer to it.

  1. Using class methods to create objects
107
Q

Examples of using class methods to create objects.

A

NSString’s + (id)stringWithFormat:(NSString *)format, … UIButton’s + (id)buttonWithType:(UIButtonType)buttonType; NSMutableArray’s + (id)arrayWithCapacity:(int)count; NSArray’s + (id)arrayWithObject:(id)anObject;

108
Q

How do you instantiate a stack?

A

Allocating and initializing an object from scratch
Doing this is a two step process: allocation, then initialization.
Both steps must happen one right after the other (nested one inside the other, in fact). Examples:
NSMutableArray *stack = [[NSMutableArray alloc] init];
CalculatorBrain *brain = [[CalculatorBrain alloc] init];

109
Q

Explain allocating.

A
Heap allocation for a new object is done by the NSObject class method + (id)alloc
It allocates enough space for all the instance variables (e.g., the ones created by @synthesize).
110
Q

Explain initializing.

A
Classes can have multiple, different initializers (with arguments) in addition to plain init.
If a class can’t be fully initialized by plain init, it is supposed to raise an exception in init. NSObject’s only initializer is init.