Category Archives: iOS

static background in uitableview

ref: http://stackoverflow.com/questions/20481118/background-image-uitableview-changes-when-scroll
http://stackoverflow.com/questions/1960603/iphone-fixed-position-uitableview-background

Objective-c’s Addresses and Pass by References

In CPU, a switch is to describe a bit. A bit represent either 0 or 1. You switch on or off. 1 or 0. Thus, its called a switch.

Eight of these bits make up a byte. Each representation of these bits make up a representation. For example, in a byte, we can have:

00000000 (0)
00000001 (1)
00000010 (2)
00000011 (3)
…etc

So as you can see, if we were to make each representation a number, we can have up to
2^8-1 representations. where 8 is the number of bits.
Currently, the example is binary.

Hexadecimal Numbers is a more complex system than using just binary or decimal and is mainly used when dealing with computers and memory address locations.

By dividing a binary number up into groups of 4 bits, each group or set of 4 digits can now have a possible value of between “0000” (0) and “1111” ( 8+4+2+1 = 15 ) giving a total of 16 different number combinations from 0 to 15. Don’t forget that “0” is also a valid digit.

Thus, a 4 bit binary number is a hex digit. If a system has 32 bits, we can say it has 8 hex digits.

If we were to work with hex, each hex digit would have 16 representations: 0123456789abcdef

In computers, data is represented by an address. An address is a hex representation.

The address of a variable is the location in memory where the value for that variable is stored.
To get the variable’s address, you use the & operator:

Notice the %p token. That’s the token you can replace with a memory address. Build and run
the program. You’ll see something like:

i stores its value at 0xbffff738

its in hex which is calculated like this

Storing Addresses in pointers

Declare a new variable named addressOfI that is a pointer to an int. Assign it the address of
i.

Getting the data at the address

Notice that the asterisk is used two different ways The first is in the declaration where you declare the variable addressOfI to be an int *. That is, it is a pointer to a place where an int can be stored.
The second is where you read the int value that is stored at the address stored in addressOfI. (Pointers are also called references. Thus, using the pointer to read data at the address is sometimes called dereferencing the pointer.)
You can also use the * operator on the left-hand side of an assignment to store data at a particular address:

Passing the Reference…by value

This is known as pass-by-reference. That is, you supply an address (also known as “a reference”), and the function puts the data there.

Changes would take place inside method ONLY if you do not move pointer

If you don’t move the parameter pointer, it would work.

For example:

objc_ref_1

In detail, here’s why its pass by value of the reference:

objc_ref_2

If you run it, you’ll see that the ptr inside the function points away to another fresh allocated integer. The outside did not move.

In order to really use a reference (similar to that of C++) to modify the data passed into a method, you need to use a double ptr:

Using pointer of pointer to make changes permanent from inside the method

Pass by Copy

As you can see, the address of our variable is different outside, than the address of the parameter variable inside test2.

Basically you just copy the value in. The parameter is basically a newly created variable on the stack with a different address, but has the same value as what you pass in.

passbyvalue

iPad: add a popover view on Navigation Bar

In your app delegate, have property for the current ViewController class:

Then in our didFinishLaunchingWithOptions method, we allocate this main ViewController, which we will insert into a newly created UINavigationController variable. We create this new UINavigationController below:

This way, our root is this navigation controller. And the navigation controller’s first page is our view controller.

View Controller

ViewController.h

Run the demo and you should have a yellow popover window appear.
Now let’s put a table that has 3 colors to choose from in there instead.

The key is that we create a viewcontroller that extends from UITableViewController. when the table view controller gets a click on a entry, it sends its corresponding UI uiviewcontroller messages via a delegate.

ColorPickerDelegate and ColorPickerTableViewController

Go Back to your ViewController and change/add the following methods:

Dismiss TextView or TextField

1) When clicking ‘Done’

Put protocol UITextViewDelegate and/or UITextFieldDelegate in there…

set up the variable….make sure you add target for the method UIControlEventEditingDidEndOnExit:

Implement the method:

2) To dismiss Keyboard on tap of the View:

view controller’s header:

view controller’s viewDidLoad:

view controller:

Finally, follow this link http://rickytsao.com/?p=1299
in order to cancel keyboard by tapping anywhere outside of it.

Apply gradient to UIView

Using gradient from one color ro another

Using a sliver of gradient as background

UIViewControllers talk to each other through delegates

The controller manages the communication between the view and the model. It takes the data from the model and communicates it to the view for display.

In the same vein, the controller also takes the changed data (due to user interaction or something else) and communicates it back to the model.

Say SmoothiesViewController and EditRecipeViewController need to communicate with each other.

Declare Protocol and use delegate variable

First declare protocol like this:

EditRecipeViewController.h

Once your protocol is set. You declare a delegate:

Whoever conforms to that delegate MUST implement the methods in that protocol. Because this class with the delegate, will be calling these methods, and the classes that conform to this protocol, must execute these methods.

In EditRecipeViewController.m it is used like this:

First, we check to see if our delegate responds to such method. If it does, then call it. This means that ALLLLL the UIViewControllers that conforms to our EditRecipeDelegate protocol must execute their method of editRecipeDidCancel.

UIViewController conforms to protocol

Say SmoothiesViewController conforms to our EditRecipeDelegate

This means in our SmoothiesViewController.m file, we must implement the required protocol methods:

Data Model (mvc)

You have your business logic in Recipes:

Recipe.h:

Recipe.m

We put our business logic into a data structure. Implement some methods that manipulate this business logic. This is called our data model.

DataModel.h:

DataModel.m – this is where we manipulate our business logic into a data structure and implement wrapper methods such as add, delete, search, indexOf…etc.

Firing events through Notifications

Model — fire events –> Controller

Our Data Model communicates with other UIViewController by firing Notifications like this in our DataModel.m:

Thus, from our DataModel, we fire a notification called FavoritesChangedNotification or RecipesChangedNotification, which are NSStrings defined in our DataModel.h like this:

Whoever is listening for these notifications will execute their @selector method code.

UIViewControllers listening for Notifications

In our UIViewController we set it up like this:

You can see we have a strong reference to the shared Data Model.

In short: the owner of an object will retain that object and is ultimately responsible for releasing it when it no longer uses that object. When an object is retained, this is called a strong reference. SmoothieViewController has a strong reference to DataModel, because it created that object. SmoothieViewController owns the DataModel for as long as it is alive (which is until this app exits) and will release it in its dealloc.

Whenever we have other UIViewControllers that updates our Data Model like RecipeDetailsViewController, we make it a so-called weak reference like this:

note:
RecipeDetailsViewController does not own the object and therefore does not retain it. Because RecipeDetailsViewController is guaranteed that the DataModel instance will be around for at least as long as it is (i.e. until the user exits the app), it has no need to retain it.

In general, if you pass along pointers to objects you should make them weak references (using the “assign” property semantics), to signify that the object you are giving it to does not assume ownership. You only use “retain” or “copy” when you are not guaranteed that the object may stick around, in which case you become responsible for releasing it when the time comes.
end note.

We then have a UITableViewController displaying the data in our dataModel variable.
What happens if our dataModel changes data? Then in our DataModel.m, these methods will fire Notification like this:

So Data Models post or fire these notifications. We have our UIViewController’s listen for them.

Our UIViewControllers listen up for notifications through [NSNotificationCenter defaultCenter]’s addObserver:

First, make sure the notification strings are set from whichever data models are firing them. In our case its in DataModel.h:

then in your UIViewController’s m file, we set up NSNotificationCenter’s addObservewr:

Once we set up the observer, we implement the method that will execute whenever we receive such a notification in our UIViewController:

What this means is that our UIViewController basically are listening for notifications from our data models. Once there is a notification, we update our view (in our case UITableViewControlelr) accordingly.

Also, remember to remove those observers in your dealloc:

Controller updates Model

Controller — updates –> Model

Now in your ViewControllers we also update our DataModel through using the self.dataModel removeRecipeAtIndex method. Our users manipulate our UITableView, and the interaction calls our commitEditingStyle:forRowAtIndexPath, which then manipulates our weak reference dataModel variable. That is how Controllers update our Models.

Working with directories and files in Sandbox

Home Directory

Naturally, you’d have the documents folder in your home directory as well:

which will give you something like this:

homeDir is: /var/mobile/Applications/DF2ACE23-C3D0-444B-9D25-7E56F102595A/Documents

This is where you save your files in your sandbox.

Writing text to a txt file

First, you have to get the name of your path + file. Something like this:

/var/mobile/Applications/DF2ACE23-C3D0-444B-9D25-7E56F102595A/Documents/test.txt

We do this by appending our file name to the home directory. In our case, let’s say self.filename is test.txt:

Then you use that path string and use method writeToFile: to write contents to that file:

full method:

Reading text from file in sandbox

Writing image to your sandbox

We first create an image. Make sure the image resource exists.
Then we give it a string name and then make sure homeDir is same as we’ve displayed above. Plug in and use like so:

where myFile.homeDir is: /var/mobile/Applications/DF2ACE23-C3D0-444B-9D25-7E56F102595A/Documents

Reading images from your sandbox

Used like this:

Implementation: