iPhone Programming Tutorial - Populating UITableView With An NSArray
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
The goal of this tutorial is to show you how to populate a UITableView with data from an array of objects. This will be the building block to display XML data as well as SQL data.
The theme of this application will be fruit. We will create an array of “fruit” objects that have some additional information besides the name. We will populate a UITableView with the names of the fruits. When the user selects a fruit from the list, the view will transition to another one and display specific details about that fruit.
I will try to be as detailed as possible however it would be useful if you have completed the following tutorials as I will use concepts from each of them:
- Create a Navigation-Based Application
- Create a New Fruit Class Object
- Create and Populate an NSArray
- Add a New View To Your Project
- Connect The View To Code
- Populate A UITableView With Object Data
- Transition To New Views And Show Data Based On The Row Selected
We are going to create our “fruit” objects that will be used in the application. If you are not too familiar with object oriented programming…Google it. That tutorial would be a little too huge for me to write.
Click on File -> New File… The object we are creating will inherit from NSObject, so select NSObject Subclass and click Next.
The next screen will ask you to name it. Go ahead and name it “Fruit” and make sure that “Also create Fruit.h” is checked. It should look like the screen below. Then, click Finish.
Now, we are going to define the properties of a “Fruit” object. For this application a fruit will have a name and a description. Open Fruit.h and add the following code:
We have created the properties needed to represent our fruit. There is one line that you may be unfamiliar with. The line -(id)initWithName:(NSString*)n description:(NSString *)desc; is a definition for a function. This function will be called to initialize a Fruit object. All NSObjects have an init method, but we want to create our own so that we can pass in a name and description when the object is created.
Open up Fruit.m and add the following code:
Here we implement the initWithName method. The code here seems pretty straight forward. We are basically setting our local copies of name and description to the arguments passed to this method. The important thing to notice here is the return self line. This is crucial for using this method as a constructor. It allows this function to return the newly created instance of a fruit object.
Next, we are going to create an array of fruit objects. Open up FruitAppDelegate.h and add the following code:
All we are really adding here is an NSMutableArray property. I used NSMutableArray instead of NSArray because it has a few more methods making it more flexible.
Now, open up FruitAppDelegate.m and add @synthesize fruits to the top. This is so other objects will have access to the fruits array. Also, be sure to include the import statement for Fruit.h.
Now add the following code to the applicationDidFinishLaunching method.
What we are doing here in the 1st three lines is creating new instances of a fruit object. Notice that instead of calling init, we are calling the initWithName method that we created. This is allowing us to pass in a name and a description to each of the fruits.
The next line [self.fruits = [[NSMutableArray alloc] initWithObjects:apple,orange,watermelon,nil]; builds a new array from the objects we just created. It is important to pass in nil as the last argument in an NSMutableArray. It will not work unless you remember this.
Now we are going to create the view that will be displayed when the user selects a fruit. Double click on any one of your .xib files to open up Interface Builder.
Click File -> New and select view and click choose.
You should now see the new view (it will be blank) and the objects associated with it. We need some sort of text field to display the description of the fruit. For this tutorial I have chosen a UITextView as opposed to a UITextField. This is because a UITextView is multi-line and is great for displaying more than one line of text. So, go ahead and drag it on to your view. Your view window should now look like this:
Now click File -> Save. One important thing to keep in mind is to make sure you are saving it in the current project’s directory. It seems that every time I add a view to a project, the default directory is not my project’s directory. Name this file FruitViewController and click Save.
Another window should show up after you click save. It is asking you if you want to add this view to your project. Just check the box net to Fruit ,click Add, and close Interface Builder.
Close Interface Builder and go back to X-Code.
We need to create a ViewController to handle our View. Click File -> New File… Select UIViewController subclass and click Next.
Name this file FruitViewController.m and check the box that says “Also create “FruitViewController.h” . Click Finish.
Now we will declare an Interface Builder Outlet for the UITextView that we added to the view. Open up FruitViewController.h and add the following code.
This line allows us to associate the fruitDescription property with the UITextView we created. Open up FruitViewController.m and add this code underneath @implementation keyword. This creates default “getter” and “setter” methods for the fruitDescription property.
Double click on FruitViewController.xib to open it in interface builder. We need to connect the view to the FruitViewController class we just created. Click on the File’s Owner object.
Click Tools -> Identity Inspector. Select FruitViewController from the dropdown next to class. Notice that under the Class Outlets section you will see the UITextView property we created.
The last step in interface builder is to connect the UITextView. Click Tools -> Connection Inspector. You should see an Outlet that says fruitDescription. Click in the corresponding circle and drag it over the UITextView on your view and release it.
Now, click the circle next the word View under outlets and drag it to the View object inside of the window that says FruitViewController in the title. When you are done the screen should look like this:
Now close Interface Builder.
The first thing we are going to do here is create a property for our new view so that it can be transitioned to when a fruit gets clicked on. Open RootViewController.h and add the following code:
We are just creating a property for the fruitViewController that we added to the project. Also note that I added the #import “FruitViewController.h” line. this will allow us to create new instances of the FruitViewController object.
Now open the RootViewController.m and find the numberOfRowsInSection method. This method tells the UITableView how many rows it will be displaying. In our case it will be the size of the array of fruits. Add the following code (click the image to enlarge):
The first line allows us to gain access to the appDelegate of our application. This is where we defined the fruit array. Once we have access to the delegate the count property of the fruit gets returned.
Now find the cellForRowAtIndexPath method and add the following code:
So the first line we added is the “FruitAppDelegate *appDelegate…” line. Again, this is giving us access to the appDelegate object where we declared the fruit array. The next line calls the objectAtIndex method on the Array of fruits. The index we will be using can be accessed via indexPath.row. This is an integer value representing each row of the UITableView. Finally, we call the setText method of the cell, to display the name of the fruit in each cell at the given index.
This is the last step. We are going to detect which row in the UITableView the user selected. Find the method called didSelectRow and add the following code(click image to enlarge).
This method gets called every time a user taps on a cell in the UITableView. The parameter indexPath has a property called row that is the integer value of the cell the user clicked on. To access this, we call indexPath.row.
The first line again gives us access to the appDelegate. The next line indexes into the fruits array and makes a copy of the selected fruit object.
The next section starting with “if(self.fruitView == nil)”, initializes the viewController if it hasn’t already been initialized (see my previous tutorial if you need more of an explanation on this). One thing to take note of: Make sure that the parameter you pass to initWithNibName matches the name of the .xib file you used for your view. So in our case, its FruitViewController.
Following this line is the line that pushes the viewController on to the navigationController stack. This causes the view to transition to the new view.
The last 2 lines pass the fruit information to the new view. They are fairly self explanitory. We first set the title of the view to the name of the fruit and then set the description text to the description of the fruit.
Now click Build and Go and your app should launch. Here are some screenshots of how it should look.
And after clicking on a fruit…
Well, I hope that you got a lot out of this tutorial. As always, if you have any questions or comments, feel free to leave them in the comments section of this post. We also have a forum to help you will all of you iphone related questions. If you get lost, you can download the sample code here
Happy iCoding!
- Posted by Brandon on 8 Aug 2008 in Interface Builder, iPhone Programming Tutorials
- Digg |
- Del.icio.us |
- Stumble |
220 Responses
Sebastien Says:
August 8th, 2008 at 10:19 pm
another nice Tutorial. I’m very newbie in Iphone Dev and Objective-C. I’ve read the “Objective-C programming” at the apple Dev Center but I need alot of source code to understand so I can’t wait for the next tutorial!
I have some problems to understand when and how to use “@property” (retain,copy, etc). The next thing i need to practice is the connections in the Interface Builder.
Thank you for your great tutorials!
bobcubsfan Says:
August 9th, 2008 at 2:12 pm
Great tutorial! Thanks. What I am learning among other things is how picky coding is in regard to punctuation and capitalization. Very easy to make mistakes and generate errors.
stowns Says:
August 9th, 2008 at 6:25 pm
honestly…you’re the shit. I have zero experience with C and had no clue where to start. Your tutorials have seriously sped things up. Thanks for taking the time to spread the knowledge
iphoner Says:
August 10th, 2008 at 5:28 am
another great one. thankyou. It would be really good to see some tutorials on creating a musical instrument. Apple did have source code for a ‘Kalimba’ instrument on their dev site but this has since been taken down.
Phil Says:
August 10th, 2008 at 8:50 am
Thanks Brandon, another great tutorial - would it be possible for you to do a tutorial that focuses on the UIScrollView using the mulitouch to zoom in / out images etc?
stowns Says:
August 10th, 2008 at 6:05 pm
one thing i would love to see added to this tutorial is a summary paragraph at the end. Something that sums up how all the files interact upon execution. But only because i’m a nub
Leo Says:
August 11th, 2008 at 2:23 am
First of all: thanks for this great series of tutorials. They’re very helpful for me!
One question regarding the “cellForRowAtIndexPath” function in this tutorial:
FruitAppDelegate *appDelegate = (FruitAppDelegate *)[[UIApplication sharedApplication] delegate];
Fruit *f = (Fruit *)[appDelegate.fruits objectAtIndex:indexPath.row];
[cell setText:f.name];
[f release];
I understand why you release the “f” object. But I don’t understand why you don’t do the same with the “appDelegate” object. Shouldn’t that object be released from memory as well? Or is it somehow being released in a different way, by a different function? Thanks for any insight you can provide!
Brandon Says:
August 11th, 2008 at 9:39 am
The reason the appDelegate object does not get released is because we are not building a new instance of it. All we are doing is getting a reference to it. If you release it, you will be releasing your application’s appDelegate.
I hope that helps.
bobcubsfan Says:
August 11th, 2008 at 5:11 pm
Brandon, I used the tutorial as a model to build an app, and ran into a problem.
When the array items exceed what the screen can show, and I “scroll” up, the program locks up.
I extended your array, and got the same results.
Any ideas?
Brandon Says:
August 12th, 2008 at 12:16 pm
Hrmm… That is odd. I’ll try to mess with it when I get some time. In the mean time, if you figure it out let me know.
thanks
bobcubsfan Says:
August 12th, 2008 at 12:53 pm
this is the change I made to your code that causes the problem.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
Fruit *apple = [[Fruit alloc] initWithName:@”Apple” description:@”Red Delicious are my favorite”];
Fruit *orange = [[Fruit alloc] initWithName:@”Orange” description:@”MMM…Fresh squeezed orange juice”];
Fruit *watermelon = [[Fruit alloc] initWithName:@”Watermelon” description:@”My favorite flavor of Jolly Ranchers”];
Fruit *apple2 = [[Fruit alloc] initWithName:@”Apple” description:@”Red Delicious are my favorite”];
Fruit *orange2 = [[Fruit alloc] initWithName:@”Orange” description:@”MMM…Fresh squeezed orange juice”];
Fruit *watermelon2 = [[Fruit alloc] initWithName:@”Watermelon” description:@”My favorite flavor of Jolly Ranchers”];
Fruit *apple3 = [[Fruit alloc] initWithName:@”Apple” description:@”Red Delicious are my favorite”];
Fruit *orange3 = [[Fruit alloc] initWithName:@”Orange” description:@”MMM…Fresh squeezed orange juice”];
Fruit *watermelon3 = [[Fruit alloc] initWithName:@”Watermelon” description:@”My favorite flavor of Jolly Ranchers”];
Fruit *apple4 = [[Fruit alloc] initWithName:@”Apple” description:@”Red Delicious are my favorite”];
Fruit *orange4 = [[Fruit alloc] initWithName:@”Orange” description:@”MMM…Fresh squeezed orange juice”];
Fruit *watermelon4 = [[Fruit alloc] initWithName:@”Watermelon” description:@”My favorite flavor of Jolly Ranchers”];
Fruit *apple5 = [[Fruit alloc] initWithName:@”Apple” description:@”Red Delicious are my favorite”];
Fruit *orange5 = [[Fruit alloc] initWithName:@”Orange” description:@”MMM…Fresh squeezed orange juice”];
Fruit *watermelon5 = [[Fruit alloc] initWithName:@”Watermelon” description:@”My favorite flavor of Jolly Ranchers”];
self.fruits = [[NSMutableArray alloc] initWithObjects:apple,orange,watermelon,apple2,orange2,watermelon2,
apple3,orange3,watermelon3,apple4,orange4,watermelon4,apple5,orange5,watermelon5,nil];
Hanno Says:
August 13th, 2008 at 1:06 am
Thx Brandon for that tutorial.
One question:
what is the difference in accessing an objects properties or methods via foo.property to [foo property]?
BrewersFan Says:
August 13th, 2008 at 11:23 am
Brandon thank you for tutorial.
I am experiencing the same issue bobcubsfan. Both my simulator and hardware lock up when I scroll quickly through list. I don’t have any insight as to why.
Also does anyone know how to access/convert the fruitDescription as a NSString?
Maybe:
NSString *str = fruitDescription.text
bobcubsfan Says:
August 13th, 2008 at 2:36 pm
When running on my iPhone, the program quits after scrolling twice without making a selection.
bobcubsfan Says:
August 13th, 2008 at 3:27 pm
This might be a clue:
The debugger (GDB) was launched when the program bombed.
bobcubsfan Says:
August 13th, 2008 at 6:05 pm
Hey Brewers fan.
I added NSString *mydesc; to
RootViewController.h
@interface RootViewController : UITableViewController {
FruitViewController *fruitView;
NSString *mydesc;
and the second @property …
@property(nonatomic,retain) FruitViewController *fruitView;
@property(nonatomic,retain)NSString *mydesc;
then,
in RootViewController.m after the line in the (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
method
[self.fruitView.fruitDescription setText:[fruit description]];
I added this:
mydesc =[fruit description];
no errors, so I guess it works.
Brandon Says:
August 13th, 2008 at 8:03 pm
Hmmm… I’m still working on the issue of the simulator locking up when scrolling. I have compared my code to numerous examples by Apple and can’t seem to see any significant difference.
Go ahead and post this issue in the forums and see if anyone has a solution.
bobcubsfan Says:
August 13th, 2008 at 10:59 pm
Hey Brewers fan. I forgot this in RootViewController.m
add the @synthesize mydesc; line.
@implementation RootViewController
@synthesize fruitView;
@synthesize mydesc;
BrewersFan Says:
August 14th, 2008 at 7:37 am
Thanks bobcubsfan!!! I appreciated you posting NSString code.
I hope Brandon or message board feedback can resolve the lockup issue.
bobcubsfan Says:
August 14th, 2008 at 7:58 am
Hey Brewersfan, you are welcome. Did it work for you? I am troubleshooting the lockup problem as well.
AstrosFan Says:
August 14th, 2008 at 8:02 am
Hi. I was wondering how would I be able to use this tutorial but at the same time, have a tab bar at the bottom. I already made a tab bar application but it doesn’t have the RootViewController or anything. Should I just go ahead and make one? Or is there another way?
mainstreetmark Says:
August 14th, 2008 at 4:56 pm
Hey dude -
This is exactly the topic and scope I was looking for. I have successfully made a list of stuff.
I eventually hope to write a native app for itunesregistry.com, but I’ve got nearly no experience in Cocoa, so this will be a learning experience…
jonas Says:
August 15th, 2008 at 6:07 am
Thank you for excellent articles! Seems like I will have to go brush off some dust from an Objective C book thats lying around here somewhere — your postings really gave me the inspiration for doing so!
Keep up the great work!
daniel Says:
August 15th, 2008 at 6:12 am
why in “cellForRowAtIndexPath” method of RootViewController.m need add
[f release]
but not need release “fruit” in didSelectRowAtIndexPath
they also have use the same way (also from fruits NSMutableArray) to get the ref.
[Fruit *fruit = (Fruit *)[appDelegate.fruits objecctAtIndex:indexPath.row];
BrewersFan Says:
August 15th, 2008 at 8:25 am
Hey bobcubsfan, I was able to get it to work kinda..
Basically I’m trying to let the user email the description once they have selected the fruit. I have an email button on FruitViewController.xib.
Here is my code:
RootViewController.h
@interface RootViewController : UITableViewController {
FruitViewController *fruitView;
NSString *mydesc;
}
@property(nonatomic,retain) FruitViewController *fruitView;
@property(nonatomic,retain) NSString *mydesc;
@end
RootViewController.m
@implementation RootViewController
@synthesize fruitView;
@synthesize mydesc;
[self.navigationController pushViewController:self.fruitView animated:YES];
self.fruitView.title = [fruit name];
[self.fruitView.fruitDescription setText:[fruit description]];
mydesc =[fruit description];
FruitViewController.h
@interface FruitViewController : UIViewController {
IBOutlet UITextView *fruitDescription;
NSString *mydesc;
}
@property(nonatomic,retain) IBOutlet UITextView *fruitDescription;
@property(nonatomic,retain) NSString *mydesc;
- (IBAction)EmailFruit;
@end
FruitViewController.m
@implementation FruitViewController
@synthesize FruitFruitFruitDescription;
@synthesize mydesc;
-(IBAction)EmailFruit {
NSString *str1 =@”mailto:?&subject=Fruit&body=”;
//NSString *str2 =mydesc;
NSString *SMessage =[NSString stringWithFormat:@"%@%@",str1,mydesc];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:SMessage]];
}
When I run this the text field of the email should read description however I get (Null).
Any idea how to fix???
Thanks again for your HELP…
Brandon Says:
August 15th, 2008 at 9:17 am
I think I have solved it!
After reading the post left by daniel, I decided to investigate my choice for releasing the f object.
After some inspection, I realized that the “f” object wasn’t a new instance of Fruit, but a reference to an existing Fruit object.
Releasing this object, removes the Fruit at that cell from memory and causes the app to crash.
So to fix the crashing problem, remove the
[f release];
line from cellForRowAtIndexPath method inside of RootViewController.m.
Thank you to daniel for bringing this to my attention. I will update the tutorial later today.
bobcubsfan Says:
August 15th, 2008 at 10:54 am
Brandon, you rock! It always seems to be the case that one small thing is the source of a bug!
Brandon Says:
August 15th, 2008 at 12:32 pm
Thanks Bob,
yea it’s always something dumb that you overlook. So I assume this fixed the bug for you then?
bobcubsfan Says:
August 15th, 2008 at 3:47 pm
Indeed it did. When I was looking at the code, I was kind of wondering about the “f release”, but am still too much of a novice to have nailed it down.
BrewersFan Says:
August 16th, 2008 at 7:55 am
Hey I got my code to work bobcubsfan. Thanks again for the post. Brandon thanks for the fix.
This is a GREAT BLOG and you guys have been a big help.
BrewersFan Says:
August 16th, 2008 at 10:44 am
Does anyone have idea on how to add search bar below list to search array? My list in my app is long and search feature would benefit user.
bobcubsfan Says:
August 16th, 2008 at 2:47 pm
Apple has sample code for a table search with an array. If you want, I can e-mail the dmg to you.
bobcubsfan Says:
August 16th, 2008 at 4:24 pm
Brandon,
Once a value has been passed, how is the value stored for use elsewhere?
For example, selecting Apple, yields Red Delicious are my favorite in [self.fruitView.fruitDescription setText:[fruit description]];
So, if I want to use the result, how can I store in and where?
Brandon Says:
August 16th, 2008 at 7:29 pm
@bobcubsfan,
Im not sure I understand your question. Are you asking me how you would store the description of the selected item?
Let me know so that I can further assist you.
bobcubsfan Says:
August 16th, 2008 at 10:37 pm
I am used to languages that have global variables. For example, declaring a global or public string makes it available everyone, while a private variable is available only in the routine, or method where it is declared. So, maybe I want to capture the description and use it somewhere else, perhaps to just use a substring of it.
I am thinking that maybe variables declared in the “delegate” and made available by the code in RootViewController
FruitAppDelegate *appDelegate = (FruitAppDelegate *)[[UIApplication sharedApplication ] delegate];
would do the job.
BrewersFan Says:
August 17th, 2008 at 3:01 pm
Hey bobcubsfan,
in the
RootViewController.m
I changed:
mydesc =[fruit description];
TO:
self.fruitView.mydesc =[fruit description];
This changed (null) to the description…
Also thank you for the offer to email table search with an array from Apple.
I have looked at example, specifically 3_SimpleIndexedTableView.
I would like to add the search functionality to this list however I am just not proficient enough to navigate the code changes..
I was fishing to see if someone with more proficiency might know how. This way I don’t have to pull all of my hair out…
marcus Says:
August 18th, 2008 at 9:32 pm
Should this compile without warngings? I get warnings in the FruitAppDelegate.m on these lines of code
Fruit *apple = [[Fruit alloc] initWithName:@”Apple” description:@”My favorite is green”];
because it says no ‘initwithName:description:’ method found.
Did I miss a step? I’m import “Fruit.h” into the FruitApp Delegate.m. When I build and go I just get a black screen and it goes into debug mode.
Brandon Says:
August 19th, 2008 at 7:05 am
@marcus,
If you are getting this warning, it probably means that you did not declare this method inside of your fruit.h.
- (id) initWithName:(NSString*)n description:(NSString*desc);
Let me know if that solves your problem. You also may have missed the step where you implement this method inside of fruit.m
marcus Says:
August 19th, 2008 at 10:10 am
Thanks for the help Brandon. I did declare it in fruit.h. Here is my fruit.h. Notice iniWithName not initWithName. Ooops. It’s working now. Figured it had to be something simple but obscure.
#import
@interface Fruit : NSObject {
NSString *name;
NSString *description;
}
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *description;
- (id)iniWithName:(NSString *)n description:(NSString *)desc;
@end
Brandon Says:
August 19th, 2008 at 10:23 am
hehe great to hear you got it working. I hate type-o errors, they take so long to troubleshoot.
Thanks for being a part of this site!
marcus Says:
August 19th, 2008 at 10:45 am
Well, I spoke too soon. It comes up fine and displays the fruits but then when I click on a fruit it crashes. It’s crashing on the line:
self.fruitView.title = [fruit name];
in the did select a row function.
Any ideas?
iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1 | iCodeBlog Says:
August 19th, 2008 at 11:46 am
[...] while still explaining the new stuff in detail. I will assume that you have completed the fruits tutorial and it’s [...]
bobcubsfan Says:
August 19th, 2008 at 12:11 pm
Marcus
Try commenting the line with the error.
//self.fruitView.title = [fruit name];
See if the error “drifts”.
Brandon Says:
August 19th, 2008 at 12:38 pm
@Marcus,
You can open up the error terminal while your application is running. It will give you a detailed log of the error. This should give you some insight as to why your app crashed.
You can access it by clicking the GDB icon inside of XCode (its located on the middle toolbar and looks like a terminal screen) after clicking Build and Go.
Paste the error output in the comments so we can see what the problem is.
marcus Says:
August 19th, 2008 at 4:54 pm
Here is the gdb output. Thanks again for your help.
[Session started at 2008-08-19 16:53:59 -0700.]
2008-08-19 16:54:02.463 Fruit[4359:20b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil’
2008-08-19 16:54:02.465 Fruit[4359:20b] Stack: (
2530500939,
2517487867,
2530500395,
2530500458,
2422354896,
2421430788,
2421430548,
816674028,
816673653,
8806,
816434578,
816463537,
816274443,
816209415,
816206378,
829003042,
829012108,
2530002453,
2530004216,
829005112,
829005309,
816175835,
816221412
)
[Session started at 2008-08-19 16:54:02 -0700.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-960) (Sun May 18 18:38:33 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “i386-apple-darwin”.warning: Unable to read symbols for “/System/Library/Frameworks/UIKit.framework/UIKit” (file not found).
warning: Unable to read symbols from “UIKit” (not yet mapped into memory).
warning: Unable to read symbols for “/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics” (file not found).
warning: Unable to read symbols from “CoreGraphics” (not yet mapped into memory).
Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/not_too_shabby/Library/Application Support/iPhone Simulator/User/Applications/6B90C11F-BA39-4AF0-A7B0-8F8495CE80BE/Fruit.app/Fruit’, process 4359.
(gdb)
Brandon Says:
August 19th, 2008 at 7:36 pm
Hrmm… That is strange. It’s so hard to troubleshoot without seeing your code. I will post up the sample code for this tutorial and you can compare it to yours.
BrewersFan Says:
August 21st, 2008 at 7:37 am
Hey bobcubsfan, Brandon,
I am trying to add search feature to array. Specifically I am attempting to add functionality from 3_SimpleIndexedTableView Apple example with side letter search. Basically my code is generating a bunch of errors.
I ‘m wondering if there is a better approach? Bobcubsfan you mentioned Apple has sample code for a table search with an array. Which the one are you’re referring to?
At the end of the day I am looking for a cleaner easier way to add search to this app. It doesn’t have to be side list. I would actually prefer search bar. Do you know of example of this? Can you send my way?
Any help with respect to this is appreciated.
BrewersFan
bobcubsfan Says:
August 21st, 2008 at 7:47 am
Assuming you have access to Apple Dev, here is the link
If not, give me your e-mail and I will send you the DMG for TableSearch
Chris McIntosh Says:
August 21st, 2008 at 10:30 am
I am having an issue with transitioning from the main view to the second view, it errors out giving this: -[RootViewController taskViewC]: unrecognized selector sent to instance 0×44f8b0′
I am actually extending the sqlLite app, but figure this would be a good spot to start as this is where the transitioning code i based it on was from.
BrewersFan Says:
August 21st, 2008 at 10:32 am
Thank bobcubsfan,
for some reason I did not see TableSearch Code last time I look in Dev Center Site.
I will give it a try and let you know.
thanks again…
Chris McIntosh Says:
August 21st, 2008 at 10:39 am
okay, fixed it. I forgot to add a synthesize line in for my view.
Chris McIntosh Says:
August 21st, 2008 at 1:46 pm
Question about libraries. Cant get into the forum because it wont accept my password. Anyway:
Does anyone know what the library is for the iPhone’s camera functions and if there is some docs anywhere on accessing it?
Brandon Says:
August 21st, 2008 at 2:14 pm
Why won’t it accept your password? It should have emailed you a temporary password. Sometimes if you do a copy and paste from this email, you get an extra space at the beginning or end that causes an error.
marcus Says:
August 21st, 2008 at 9:25 pm
Thanks for posting the code. I missed the “==” in the nil comparison. I only had a single = sign. If I had a nickel for everytime that has messed me up.
BrewersFan Says:
August 22nd, 2008 at 5:04 pm
All, bobcubsfan, Brandon,
Does anyone know how to change font size / make text in UITableView wrap around? My “Fruit” names in my array are long and I get this:
Applelongfruitname….
BrewersFan
Bob Schoenburg Says:
August 22nd, 2008 at 5:28 pm
Does this help?
http://discussions.apple.com/thread.jspa?threadID=1623586&tstart=70
BrewersFan Says:
August 23rd, 2008 at 5:26 am
Bob,
thanks for post. I am looking to do what is guys is asking. I have looked at the apple sample code on this and still can’t figure out.
It seems like this should be easier to implement..
In my tableview I can change
background color
size of table
but not the font size or create word wrap..
Still confused…
Sam Wilson Says:
August 27th, 2008 at 9:00 am
Awesome tutorials, one question… why did you do this:
IBOutlet UITextView* fruitDescription;
instead of this:
IBOutlet UITextView *fruitDescription;
Are they different?
Brandon Says:
August 27th, 2008 at 11:47 am
Sam,
I did do it like this:
IBOutlet UITextView *fruitDescription;
I don’t think it would compile the way you are suggesting that I coded it.
John Says:
August 27th, 2008 at 12:00 pm
Brandon,
Thanks for the great tutorial. One quick question–how does one change the title of the back button, so that when “Watermelon” is selected, the back button reads something else, like “Index”, instead of “Fruits”?
Any help would be appreciated!
Brandon Says:
August 27th, 2008 at 12:04 pm
The title of the back button is determined by the title of the view you are going “back to”. So if you want it to say Index, title your rootViewController “Index”
So where I have self.title = @”Fruits”; you could change it to self.title = @”Index”;
Hope that helps!
John Says:
August 27th, 2008 at 2:47 pm
Brandon,
Thanks for the help–that works, but it changes the title too. I’m trying to do something like the Facebook app, where it shows “facebook” on the Navigation Bar, but “Profile” or “Chat” once the next view is loaded. Is there any way to disconnect the back button title from the title of the previous view?
John
PS - I was looking at the documentation of the UINavigationItem class and its backButtonTitle property, but I’m pretty new to this and unsure *when* to change this property and from which view controller…
Bob Schoenburg Says:
August 27th, 2008 at 3:42 pm
Try this:
comment the line:
self.fruitView.title = [fruit name];
in RootViewController.m
then and this in FruitViewController
//If you need to do additional setup after loading the view, override viewDidLoad.
- (void)viewDidLoad {
self.title = @”Chat”;
}
Adam Says:
August 31st, 2008 at 4:54 am
Brandon,
Is it necessary to release the NSMutableArray *fruits in dealloc method as shown below?
- (void)dealloc {
[fruits release]
[navigationController release];
[window release];
[super dealloc];
}
john Says:
August 31st, 2008 at 9:11 pm
Great stuff here!
I am trying to extend the fruit example, with only 1 change, but that is enough to stump me
I just want to have 2 UITableViews on the main page (each 1/2 of the view). Is this possible starting with the ‘nav based app’ that we are using for the fruit example? I am not sure how to change the name of the original one ‘tableView’ If I could rename it to toptableView, I might be able to figure it out
Any pointers from anyone?
Brandon Says:
August 31st, 2008 at 9:38 pm
@Adam,
Its not necessary but great practice. I was a little lazy and should have put it in. Great catch!
Jim Says:
September 4th, 2008 at 1:01 pm
First of all: thanks for this great tutorials. It’s very helpful for me!
Following the tutorial, I created the Fruit project. Everything was fine until I pushed a fruit item from the fruit table. I got the following problems.
Fruit[6749] *** Assertion failure in -[UILabel setTextColor:], /SourceCache/UIKit/UIKit-738/UILabel.m:288
Fruit[6749] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid parameter not satisfying: color’
Any pointers to this are appreciated?
Jim
Jon K Says:
September 6th, 2008 at 4:07 pm
Again, great tutorial! Thank you!
1 suggestion. In the ‘didSelectRowAtIndexPath’ method I set the title and description of the fruitView first, then added it to the navigationController stack. If you add the view to the stack first, you will see the title of a previously viewed fruitView just for a split second and then it will change the title.
AndyB Says:
September 20th, 2008 at 10:33 am
@Jon K
I just tried the change you suggested and changed the code in didSelectRowAtIndexPath to look like this:
self.fruitView.title = [fruit name];
[self.fruitView.fruitDescription setText:[fruit description]];
[self.navigationController pushViewController:self.fruitView animated:YES];
It fixes the problem with the title display but…
The problem is that when I run the app the first time I click on a fruit the description is blank. After that every time a fruit is selected the correct description is shown.
Is there something else I need to change to make this work?
Thanks.
corey Says:
September 22nd, 2008 at 2:37 pm
In the method:
- (void)viewDidLoad
you delete the line where you call super:
[super viewDidLoad];
Is it not necessary to call super if you implement the method?
Thanks for al the help on your blog….
Brandon Says:
September 22nd, 2008 at 3:49 pm
It’s good practice, because it will be required if you overwrite more complex functionality. However, it’s not 100% necessary.
billgajen Says:
September 23rd, 2008 at 2:23 am
Hi,
Its amazing.. It helps me a lot. Thank you so much.
But I have doubt,
How to do like UITableView to UITableView..
I mean, in the table when you tab on a cell that should bring another table. for example 1st one is main menu then when you choose one that should give you a submenu! I know its possible
but if can let us know pls
Pls
Mike Says:
September 26th, 2008 at 7:30 am
Brandon - thanks for the example…..I do have one question though. I am trying to show two fields in the cell.text and seem to be having problem….basically, this is what I am trying to do:
cell.text = f.firstname && f.lastname;
Question is - how do I concatenate two fields in C - I am not too familiar with C. Also what if one of these fields was a date field?
For eg:
cell.text = f.name && f.dateofbirth
Thanks for your help!!
Mike
Brandon Says:
September 26th, 2008 at 8:09 am
@Mike,
Appending strings in Objective-C is a little different than in other languages. I don’t know why Apple just didn’t make it so you would write String foo = foo + bar.
So to append string you would do something like this:
NSString *name = [[NSString alloc] initWithString:f.firstname];
[name stringByAppendingString:f.lastname];
cell.text = name;
Does this make sense? It’s quite a bit of code just to append a string to another string. Let me know if you need more help.
billgajen Says:
September 27th, 2008 at 4:02 am
Hi brandon,
Why you ignored my qustion?
I am trying to do some thing in apps. as a beginner I need some help
pls help me
Brandon Says:
September 27th, 2008 at 7:42 am
@billgajen
Sorry, it took me so long to respond. The question you are asking would take more than just a few sentences to answer. In fact, I could do an entire tutorial on it.
I get a lot of requests for such a functionality and will consider doing a tutorial on it in the future.
theluiz Says:
September 28th, 2008 at 2:31 pm
Hello…
I followed your code exactly only naming all the variables and files differently, and there are no errors or warning when building; however the UITableView is empty! :O
I looked through the code and found no differences to your code (only with my variables)… Please help!
theluiz Says:
September 28th, 2008 at 2:42 pm
ahh nvm found it…
forgot the
return self;
in
- (id)initWithName:(NSString*)n subject:(NSString *)subj description:(NSString *)desc duedate:(NSString *)due {
Brandon Says:
September 28th, 2008 at 2:48 pm
@theluiz,
haha nice. It’s always something simple… I’m glad you were able to get it working.
iPhone Application And Website Development: All Tools And Tutorials You Need | Athena Design - The Lounge Says:
September 29th, 2008 at 12:26 am
[...] Populating UITableView With An NSArray [...]
theluiz Says:
September 29th, 2008 at 8:15 am
QUESTION : ![]()
how would i add an object to this array? thanks in advance!
Brandon Says:
September 29th, 2008 at 9:19 am
@theluiz,
It’s pretty simple.
Fruit *peach = [[Fruit alloc] initWithName:@”peach” description:@”fuzzy on the outside”];
[self.fruits addObject:peach];
Does this make sense? I’m on the city bus responding from my iPhone so sorry if there are any typos. Let me know if you need more help.
theluiz Says:
September 29th, 2008 at 9:38 am
ah ok makes perfect sense… thanks alot for the replies!
theluiz Says:
October 1st, 2008 at 6:43 am
ok one more question (maybe the last ^_^)…
how would i add an uiimage to the list of
NSString *name;
NSString *description;
such that when i add an object to the array, i just need to specify
initWithName:@”hello” description:@”hi” picture:@”icon.png”
and i could then simple set a uiimageview to this image? sorry if im not making sense
wmparry Says:
October 1st, 2008 at 6:53 am
Hi,
I know this is a bit of an old subject, but just working through the tutorial and I’ve got stuck on the Connection Inspector part. I can’t get the fruitDescription Outlet to connect to the UITextView. I can connect the View to the FruitViewController, but the former just won’t play. I’ve double checked my code in FruitViewController.h and .m and I can’t spot any error except that my *fruitDescription is grey shaded like the screen shot.
Thanks in advance for any advice. Great tutorials though.
Brandon Says:
October 1st, 2008 at 9:48 am
@wmparry
Did you make sure and complete the step when you select FruitViewController from the drop down on the Identity Inspector of the File’s Owner object? This would be the first thing I would check.
@theluiz,
in Fruit.h
// Declare the UIImageView
UIImageView *picture;
// Make it a property
@property(nonatomic,retain) UIImageView *picture;
// Modify the initWithName method signature
-(id)initWithName:(NSString *)n description:(NSString *) desc picture:(UIImageView *) pic;
in Fruit.m
// synth the UIImageView
@synthesize picture;
// Update the initWithNameMethod
-(id)initWithName:(NSString *)n description:(NSString *) desc picture:(UIImageView *) pic {
self.picture = pic;
……
}
That should do it!
theluiz Says:
October 1st, 2008 at 10:53 am
ok so i made picture blank.png in my object that gets created on startup, however in didSelectRowAtIndex i put this
[self.workView.workPic initWithImage:[homework picture]];
and it does not work… i also tried this
[self.workView.workPic setImage:[homework picture]];
with no luck
wmparry Says:
October 1st, 2008 at 12:34 pm
@Brandon
Hi, yes, as far as I can see, I’ve followed the tutorial to the letter. I’ve even recreated the View from scratch. Dragging the circle from the fruitdescription outlet just won’t connect to the text view. Not sure what to do now.
Deamb Says:
October 4th, 2008 at 11:34 pm
Hello again Brandon,
Incredibly clear tutorial…! thanks
If I may, one quick question:
How complicated would it be to populate a tableview from an SQlite db retrieved on-line (so as to have up to date data ?), that could be refreshed at will a little like the currency rates in many apps ?
thanks
Josh Chaney Says:
October 5th, 2008 at 2:41 am
How can I put a hyperlink in the descriptions ( after clicking on of the rows? I’ve googled everything, and it’s not clear to me from reading any of the docs in the dev center. Or what would work even better for me would be to have each row be an actual hyperlink. Since there is no google maps integration, I’m trying to build a tableview with rows that go to certain websites.
Eric Says:
October 7th, 2008 at 3:31 pm
I feel totally dumb. How does one get a program onto the iPhone. I went to Build>Compile, but that didn’t do anything.
Brandon Says:
October 7th, 2008 at 8:37 pm
@Eric,
don’t feel dumb. It’s a legit questions. You can’t get apps on to the iPhone without paying the hundred bucks and getting accepted to the developer program. After this you will get a signing certificate to bundle with your app.
The solution for you at this point is to test the app in the iPhone simulator. At the top of XCode you should see a button that says “Build and Go”. Click that and it should launch the simulator by default.
Let me know if you have any more troubles…
mha Says:
October 8th, 2008 at 6:17 am
So next dumb (sorry legit
question is: is simulator reliable enough, that all testing can be done using this simulator only?
Serge O. Says:
October 9th, 2008 at 8:20 am
@mha
I guess the answer would be… no !
I spent quite a lot of time developping an app with a NSNumberFormatter, which worked fine on the simulator… When I built to my iPhone, it did not !
It turns out it’s a known issue…
So, you can’t be totaly sure…
Plus, apps on the simulator run super fast( mac CPU…), you only get a good idea of the fluidity of your app from running it on a device.
Eric Says:
October 9th, 2008 at 2:12 pm
Thanks for the info. I’m very new to programming and was excited to get an app out there. I was planning on the making the app free. But if I have to pony up $99 then I might just need to charge 99¢
Will Says:
October 11th, 2008 at 3:47 pm
This tutorial is great, but I think I’ve followed all the steps, and I’m getting a crash when I click on a fruit:
2008-10-11 18:42:59.372 Fruits[41233:20b] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UIViewController loadView] loaded the “FruitViewController” nib but no view was set.’
Am I missing something here? here’s my code:
FruitsAppDelegate *appDelegate = (FruitsAppDelegate *)[[UIApplication sharedApplication] delegate];
Fruit *fruit = (Fruit *)[appDelegate.fruits objectAtIndex:indexPath.row];
if (self.fruitView == nil) {
FruitViewController *viewController = [[FruitViewController alloc] initWithNibName:@”FruitViewController” bundle:[NSBundle mainBundle]];
self.fruitView = viewController;
}
self.fruitView.title = [fruit name];
[self.fruitView.fruitName setText:[fruit name]];
[self.navigationController pushViewController:self.fruitView animated:YES];
Sorry to bug you like this, but I’ve just been staring at this code and I can’t figure out what’s wrong.
I’m also getting the same problem as @wmperry above, I can’t get the bubble from my IBOutlet to connect the dots with the TextView on my custom view. Is there any chance that this tutorial is out of date with the newest version of XCode/Interface Builder? Otherwise I think I’ve missed some crucial step.
Will Says:
October 11th, 2008 at 4:07 pm
I should also add that my “view” does not look like your screenshots. For one, my window header says “Custom View” instead of “View” and the background is greyish and has text on it. Has something changed between when you made your tutorial and now? I just downloaded this Interface Builder today. I feel like there’s some step I’m missing to set this view as the main view for the Nib.
Will Says:
October 11th, 2008 at 4:11 pm
Found the problem. I was creating a “Cocoa” view instead of “Cocoa Touch”. @wmperry, was that your problem too? Even if it wasn’t, please say yes so I don’t feel like the only idiot here who didn’t catch that.
Will Says:
October 11th, 2008 at 4:24 pm
No, ok, still not working. Can you clarify something? Why are you importing “RootViewController” into your application’s delegate implementation, if you never reference it?
Will Says:
October 11th, 2008 at 4:41 pm
OK, got it- that was my mistake- i didn’t do the second step with dragging the view. Sorry bout all my wasted space comments. Keep up the good work!
Alan Says:
October 12th, 2008 at 1:58 pm
Hey - I noticed that by default when I create a nav type project - I get the following in my FruitAppDelegate.h file
@interface FruitAppDelegate : NSObject {
UIWindow *window;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
Which is the opposite of your file where your variables are of type IBOutlet and your properties are of type UIControl
I haven’t managed to get through this one yet.
Steve Says:
October 18th, 2008 at 11:19 pm
Really great tutorial! Thank you so much!
I was wondering if anyone could help me initialize the table to be one of those that have rounded corners and group the data into separate white rounded rects on a blue background. I believe it is using a style called UITableViewStyleGrouped but I cannot get it to work.
I realize that I will have to alter the NSMutableArray to divide up the data into sections but that is the second step. I would feel progress if I could just get the data in the this tutorial to appear in a rounded rect.
Of course if anyone has a sample of how to create the array that would be great as well!
Thanks again! I look forward to going through your other tutorials, this one was the best iPhone tutorial I have ever seen.
MGC Says:
October 20th, 2008 at 8:47 pm
“For this tutorial I have chosen a UITextView as opposed to a UITextBox. This is because a UITextField is multi-line and is great for displaying more than one line of text.”
You list 3 different UI objects in this phrase. Is the the 3rd one supposed to be UITextView or UITextField?
Brandon Says:
October 20th, 2008 at 9:26 pm
@MGC
Good catch, this was a type-o and I have fixed it. Thanks for pointing that out…
Kr1sh Says:
October 21st, 2008 at 5:03 am
Hi Brandon, This is a great tutorial for what I wanted to do. However, I wanted to display a webpage in UIwebView when i tap a cell in table view, for example table view will have options like google, apple. When I tap google it displays google web page in UIwebView. I tried using couple of methods like NSURL request and so on..still i cant get it to work.Im not a programmer but with all your tutorials, i got confidence that I could do something…please help me with this..Thanks
Steve Says:
October 22nd, 2008 at 9:40 am
Pardon me if I am being dense but could someone please help me alter this code snippet from my project (which is based on this most excellent tutorial) so that the table is initialized using UITableViewStyleGrouped?
I have tried everything i can think of but I seem to be messing up. I assume this is a relatively simple thing…
Any assistance would be greatly appreciated!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Park *p = (Park *)[appDelegate.parks objectAtIndex:indexPath.row];
[cell setText:p.name];
// Set up the cell
return cell;
}
Brandon Says:
October 22nd, 2008 at 11:28 am
@Steve,
What seems to be the error message that you are receiving? You would see this appear in the console…
Steve Says:
October 22nd, 2008 at 12:40 pm
Brandon,
Thanks for the quick reply!
I just realized that I had been making an error in trying to set the style of the list in the following line of code:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
rather than by simply setting the style in the root view controller using IB. Oops!
Now I am trying to figure out how to add a toolbar below the list view. Any suggestions?
Also, is there a good way to put a toolbar at the top of an application just below the Navigation controller titlebar? I am trying to make something that allows users to select a day with something like the control in the calendar app which displays a left and right arrow and the current date. I don’t see an obvious direction.
The toolbar at the bottom that I asked about above is my compromise since I have seen it so I know it can be done.
Again, I can’t tell you enough how helpful your tutorials have been!
Thanks!
SFGiantsFan Says:
October 23rd, 2008 at 11:14 am
Hi All! And thanks Brandon for a very helpful site! I really appreciate the level of detail you’ve put into it. And as I slowly go through every line I really try to understand EXACTLY what’s going on… And I think I do…except with these calls to UIApplication. Specifically:
FruitAppDelegate *appDelegate = (FruitAppDelegate *)[[UIApplication sharedApplication] delegate];
Could someone give a DETAILED explanation of what exactly is happening here? You are setting a pointer to the Application’s Delegate. But because FruitAppDelegate is a class isn’t it also an object? And as such couldn’t you just send IT a message asking it to reveal its delegate:
*appDelegate = [FruitAppDelegate delegate]
I know there’s some typecasting going on but WHY is each piece necessary?
I know this could be a junior question and I apologize in advance…but would really appreciate anyone who could clear it up for me. Thanks!
Steve Says:
October 23rd, 2008 at 9:46 pm
I am really stumped trying to figure out how extend this tutorial to add a toolbar with a couple of buttons to the bottom of the list of fruits. It should be part of that table view and thus not be visible on the fruit detail view.
Should this be done in Interface Builder or code? I have tried both without success.
Thanks!
Bill Says:
October 24th, 2008 at 7:23 pm
Brandon,
Along with everyone else, thanks for these tutorials. My question is:
Should I be able to follow this example and do everything the same except rather than using a UIViewController Subclass, use the UITableViewController Subclass?
I of course don’t use a UITextView in my class, I just used a UITableView for my IBOutlet and I created a property and synthesized that property.
As soon as I get to the line:
[self.navigationController ....] it breaks into the Debugger with no real indication as to why.
Any ideas?
ElegantMac Says:
October 24th, 2008 at 10:05 pm
Brandon, just wanted to add to the accolades of others…your tutorials are awesome and you’re always finding ways to top yourself, even w/ a heavy CS load.
Anyway, just something to add to this and your other table-based tutorials. One thing that’s helpful as a visual clue that a cell is “drill-downable” is a nice disclosure indicator. I didn’t see a way to do this with Interface Builder, but adding this to the cell’s specification works:
[cell setAccessoryType:(UITableViewCellAccessoryDisclosureIndicator)];
Added your site to my list of RSS feeds, keep up the great work!
ElegantMac Says:
October 27th, 2008 at 3:38 pm
Hi Brandon, since you mention in the FruitViewController creation that XCode never seems to add files where you want, try this:
>> In Xcode, in the project organizer select either the project (Fruit at the top of the Groups and Files list) or the Classes group folder—the new files will be added to the current selection.
Andreas Says:
November 2nd, 2008 at 6:11 am
@SFGiantsFan: Internally in Cocoa, classes are also Objects, so strictly speaking, you’re right. But classes only understand class methods (prefixed with “+”) and object instances only understand instance methods (prefixed with “+”). That’s why the class FruitAppDelegate doesn’t respond to the “delegate” method/property.
Here’s a short breakdown of what’s happening in the line:
FruitAppDelegate *appDelegate = (FruitAppDelegate *)[[UIApplication sharedApplication] delegate];
This is the same as the following sequence of statements:
// Call UIApplication’s *class* method +sharedApplication
// which returns the one and only Application object
UIApplication theApp = [UIApplication sharedApplication];
// We then ask the Application object for it’s delegate
// (a FruitAppDelegate instance)
FruitAppDelegate *appDelegate = [theApp delegate];
If I’m not mistaken, casting is not strictly necessary here.
Andreas Says:
November 2nd, 2008 at 6:12 am
Of course object methods are prefixed with a minus sign (”-”). Sorry for the typo.
Jitesh Says:
November 10th, 2008 at 4:24 am
Hi,
Thanks for the tutorials ..it really helped me a lot …
As an enhancement i want to add the fruit icons besides the name of the fruit …. same as iphone application has shown in setting option …. how can i do this … please give me reply on my mail account if possible .
Brandon Says:
November 10th, 2008 at 7:45 pm
@Jitesh,
Follow my SQLite tutorials. I detail how to do this.
Edward Says:
November 12th, 2008 at 11:00 pm
Hi.. Brewersfan wrote back in august about adding an email button to the fruitviewcontroller view window. I plugged in all the code he gave and i added the button and linked the touchdown property to the emailfruit procedure. when i click on email, it doesn’t do anything. Can someone help?
SAKrisT Says:
November 22nd, 2008 at 3:25 pm
Good work…
Thanks for the tutorial…
will be the time that something like this and seriously write
Good luck
Ricardo Says:
November 23rd, 2008 at 4:12 pm
Hello, and thanks for these awesome tutorials.
Im stuck at this part:
Now, click the circle next the word View under outlets and drag it to the View object inside of the window that says FruitViewController in the title.
I dont have any windows with FruitViewController in the title in my interface builder. Can anyone point me out to what I’m missing in this step?
Thanks.
Ricardo Says:
November 23rd, 2008 at 4:33 pm
Ah nevermind, i figured out my error! Instead of calling the view object just by view i called it table view. So i couldn’t find any object called view. Now I get it!
Joeri Says:
November 27th, 2008 at 4:26 am
I don’t know why but I’m getting the following errors in FruitAppDelegate at the following lines
Fruit *apple = [[Fruit alloc] initWithName:@”Apple” description:@”Red Delicious are my favorite”];
warning unused variable “apple”
Fruit *orange = [[Fruit alloc] initWithName:@”Orange” description:@”Mmm…. Fresh squeezed orange juice”];
warning unused variable “orange”
Fruit *watermelon = [[Fruit alloc] initWithName:@”Watermelon” description:@”My favorite favorite flavor of Jolly Ranchers”];
warning unused variable “watermelon
Everything else matches perfectly… Any ideas?
Gero Says:
December 11th, 2008 at 5:28 am
Hey Brandon.
Thanks a bunch for your very well written tutorials. Chewing through Apple´s example projects with some date here, some constants there and “oh, we prepared the Localizable.strings file so you have to switch back and forth to understand which localized string says what” are monsters to digest for beginners like me.
One Question, though.
Do you know how to pop up a tableView two steps up in the navigation hierarchy? I implemented a “Save”-Button which is supposed to bring you back to an overview tableView.
I found that you can call [self.navigationController popToViewController:upTheLadder animated:YES];
So far so good, it expects the upTheLadder refference, though and all my attempts of creating a lokal refference were futile so far, because obviously the navigationController will only pop up the upTheLadder view it already knows.
So how do I get my navigationController´s refference to that specific View?
Darryl Says:
December 16th, 2008 at 3:49 pm
Hey Brandon,
I have tried a couple of your tutorials and every time I get to the part where I am connecting the view to the class.
“Identity Inspector. Select FruitViewController from the dropdown next to class. ”
FruitViewController is not in the dropdown.
The same happened when I tried your views tutorial.
Gero Says:
December 17th, 2008 at 2:39 am
@Darryl:
You have to create a new class file and then in IB make sure you have the “File´s owner” selected.
Maybe you have to reload all class files?
Edmond Says:
December 18th, 2008 at 2:05 am
Hi Brandon,
I am new to iPhone programming, thanks for the tutorial, it helped me a lot.
I have a question regarding the fruitDescription, how to modify the fruitDescription to display content from a rtf/rtfd file?
thanks
MauiG Says:
December 18th, 2008 at 7:34 pm
Great tutorial Thanks!
In response to Luis and the UIImage View wouldn’t you need to change this code in the RootViewController.m file? I just cant figure it out. I was also trying to add a ‘phone’ and ‘url’ field as well using the same priciples? Thanks again…
[self.fruitView.fruitPicture setText:[fruit picture]];
mz Says:
December 25th, 2008 at 2:03 am
Hi Brandon. Much thanks for the hard wrk you have put in!
Question: Should dealloc in FruitAppDelegate.m have the following to release memory?
- (void)dealloc {
for (Fruit *fruit in self.fruits) {
[fruit release];
}
[self.fruits release];
[navigationController release];
[window release];
[super dealloc];
}
Todo List Application -iphone programming | iPhone App Devs Says:
December 26th, 2008 at 12:42 pm
[...] while still explaining the new stuff in detail. I will assume that you have completed the fruits tutorial and it’s [...]
Brian Says:
December 28th, 2008 at 8:26 pm
Thanks Brandon for this great tutorial.
For some reason i’m getting an error: _TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION_ when I click on any row on the first page, if I click on Apple nothing happen.
Anybody knows what is going on? Thanks in advance.
Brian Says:
December 29th, 2008 at 1:10 am
My fault, misspelled the viewController. It’s working fine now.
Rudy Says:
January 1st, 2009 at 6:20 am
Thanks Brandon for this great tutorial. I downloaded your source and it compiles and runs just fine.
As soon as I select a cell from the table view and I push the new view to the navigation controller my app crashes and I get:
2009-01-01 13:16:01.874 Logweb Touch[1330:20b] *** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.’
2009-01-01 13:16:01.876 Logweb Touch[1330:20b] Stack: (
2487161163,
2511207995,
2487159921,
2512303688,
2512302254,
2512845665,
818018567,
2487114885,
818013269,
818021560,
816606922,
816607919,
816608289,
816623645,
816632853,
816635171,
816634205,
10678,
816379725,
816409420,
816216335,
816148471,
816144856,
827735530,
827745292,
2486662677,
2486664440,
827737600,
827737797,
816114840,
816160916,
9428,
9282
)
Anybody knows what that means? I have done everything as stated in the tutorial (at least I think I did) and I have compared Brandon’s source and Nibs with mine and they look quite identical. Anybody who can help me with this issue?
Thanks in advance! And a Happy New Year everyone!
Rudy
D. Says:
January 3rd, 2009 at 12:15 am
How can I write, for exemple: I “love” Apple, with quotations?
If I write Fruit *apple = [[Fruit alloc] initWithName:@”Apple” description:@”I “love” Apple”];
I got an error. Thanks.
Diego Says:
January 4th, 2009 at 8:14 pm
Hi… I´m triying to implement an UIImageView (as you explain it to theluiz) to load a picture (located in the application) in my fruitview.
I´ve tried everything but nothing :(. Maybe could you send me a fullcode, o paste it in the website.
Thanks & Regards
C. Says:
January 11th, 2009 at 7:40 pm
Hi, I tried to do this tutorial but I keep getting the error:
Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UIViewController loadView] loaded the “FruitViewController” nib but no view was set.
I don’t know why. Could you please help me out?
Stephen James Says:
January 14th, 2009 at 9:30 pm
Fantastic Tute. I created my first app from approximately 300 tutorials, but of all of the I found yours most useful. I submitted it on Dec 30, it was sent back January 6 because of inappropriate table feedback issues (the reo turned blue and stayed blue).
I fixed that and also changed the disclosure thingies from the plain chevron to the blue circled chevron. I resubmitted on the 7th and it was approved on Jan 11. Whoo - hoo.
Oh, the name of the app is iGreyhound.
Right now I am learning how to make a tabBar pull up the three different clubs (you’ll see what I mean if you download it (for free).
I also want to learn how to store use local images - right now I know how to pull them from the web (but that would mean data charges for my unsuspecting app downloaders) or I can put one image in view. Like I said, I’m working on it.images on the resource. Thanks again for the tute.
Regards - Stephen
me Says:
January 16th, 2009 at 2:32 pm
More explanations!!!
I’ve seen so many tutorials, and each of them explained why you have to do exactly like this.
Not for this one!
You just keep saying “type this and it’ll do that!”, without any explanation.
Dezorian Says:
January 21st, 2009 at 4:07 pm
Thanks for this great tutorial! Finally found what i was looking for.
But i’m having problems though:
I’ve build the application excaclty as your samplecode (downloaded the app for comparing every file). But i can’t seem to build my app properly.
I get an error at the following line in the rootviewcontroller:
[self.fruitView.fruitDescription setText:[fruit description]]
‘error: request for member “fruitDescription” in something not a structure or union’
if i comment the line, the program runs but crashes at startup with the message “__TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__”which is thrown in the main() method.
If i run your sample code from this tutorial it runs fine. I checked every line of code and all the properties in the interface buidler. I’m getting desperate.
Do you have any ideas where to look for this? Thanks in advance.
Manish Says:
January 23rd, 2009 at 5:39 am
Hello
I am working on same type of application using XML but need to extend it like i need again a table view on second view. Application flow is like
Books category(First TableView)->Books List(Second TableView)-> Books Details (Customized View)
Dont know how to add a TableView on second view. I tried to add A TableView on second view by dragging it(also created controller for it) but got exception(…..unrecognized selector sent to instance…….).
Can anybody help me to understand it.
James Says:
January 23rd, 2009 at 10:46 am
Hi Brandon, Great tutorial.
I have been trying to add a UIImageView to each fruits page to display an image of each fruit along with the description. Im having trouble getting the image to actually appear. I have:
-given each fruit object a UIImage *fruitPicture
-added a IBOutlet UIImageView to the fruitViewController, — initialized the view and set the image
-used Interface builder to connect the outlet to the view
Could you suggest the correct way to do this?
Thanks!
MauiG Says:
January 24th, 2009 at 5:26 pm
I am having the same problem with UIImageView, and have tried everything - If anyone could help with this that would be greatly appreciated!
daniel Says:
January 30th, 2009 at 8:11 am
Why are you declare “NSMutableArray *fruits;” at fruitAppDelegate?
Can’t yoy declare at RootViewController ??
And why there are property “retain” and others “copy”
Thanks so much.
PD:sory my english!
CLAD Says:
January 30th, 2009 at 6:00 pm
help Im having trouble with this step I have done this step over about 4 times and I mean from the beginning the first go round it worked and I was able to connect and complete the app ut when I ran it it crashed so I started over I thought it would be easier HMMMMm anyways Im still a NOOB and need help here is the prob
Click Tools -> Identity Inspector. Select FruitViewController from the dropdown next to class. Notice that under the Class Outlets section you will see the UITextView property we created.
But in the Class Outlets there is no UITextView .
here are my lines of code with my differences ignite instead of fruit.
igniteViewController.h
#import
@interface igniteViewController : UIViewController {
IBOutlet UITextView *igniteDescription;
}
@property(nonatomic,retain) IBOutlet UITextView *igniteDescription;
@end
igniteViewController.m
#import “igniteViewController.h”
@implementation igniteViewController
@synthesize igniteDescription;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn’t have a superview
// Release anything that’s not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
@end
I hope somebody can help me thanks
MauiG Says:
January 31st, 2009 at 11:34 am
James,
Did you ever figure out the answer? I followed the instructions above that were given to Luiz… The only problem is in the detailed view controller. Which Luiz solved above but did not given his solution:
This will work to get the same image on each page
UIImage *img = [UIImage imageNamed:@"apple.png"];
[[[self fruitView] fruitPicture] setImage:img];
But when I try anything like these statement to pass the image it won’t work..
[[[self fruitView] fruitPicture] setImage: fruit picture];
Kevin Says:
January 31st, 2009 at 6:42 pm
This is killing me. I can’t find the answer anywhere.
How do you create an image array instead of text? I’d like to create an app similar to the one you have here, except, when the row such as “apple” or “orange” is pressed, you’re taken to a view that has the name in the nav bar, the description in the textview just like yours has, but also I’d like to add an image of the thing you pressed. The app I am working on has 220 rows. I’d like to load in an image into the view along with the description when the row is pressed. How do I do that without creating 220 nibs?
MauiG Says:
January 31st, 2009 at 8:32 pm
Kevin,
You can look at the instructions that were given to Luiz… It shows how to add an image. Then you just need to figure out the last line of code that I mentioned above. Luiz seemed to be able to figure it out…
CLAD Says:
February 1st, 2009 at 1:19 am
@Brandon first of all thanks for this tut it rocks.
I figured out the last trouble it was a simple error
but now I have just a few error’s coming up.
In RootViewController.m
the error reads
” no declaration of property ‘fruitView’ found in the interface.
here are my lines of code
#import “RootViewController.h”
#import “fruitAppDelegate.h”
#import “fruit.h”
@implementation RootViewController
@synthesize fruitView;
any ideas?
CLAD Says:
February 1st, 2009 at 2:18 am
k I got the last one to work but i cant figure this on out Im getting 2 code error’s
In the RootViewController didSelectRowAtIndexPath line
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
FruitAppDelegate *appDelegate = (FruitAppDelegate *)[[UIApplication sharedApplication] delegate];
Fruit *fruit = (Fruit *)[appDelegate.fruits objectAtIndex:indexPath.row];
“HERE ARE THE ERROR messages”.
error:syntax error before ‘)’ token
error: syntax error before ‘objectAtIndex’
so thats it any help would be great thnx
Zpeskin Says:
February 1st, 2009 at 12:30 pm
i am having a problem with a simple variation of this tutorial
When i try to push the details view i get an uncaught exception error - it says the nib file loaded fine but the view connection was not made.
I checked in IB, but the view was connected to the controller file for the second view. Any ideas?
BoxBoy Says:
February 2nd, 2009 at 10:55 pm
@Brandon I am having a problem with In the:
“RootViewController didSelectRowAtIndexPath line”
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
FruitAppDelegate *appDelegate = (FruitAppDelegate *)[[UIApplication sharedApplication] delegate];
Fruit *fruit = (Fruit *)[appDelegate.fruits objectAtIndex:indexPath.row];
“This is what the messages are saying”
error:syntax error before ‘)’ token
error: syntax error before ‘objectAtIndex’
Hey thanx for this Tutorial is awesome man and for your reply.
Fabio Says:
February 5th, 2009 at 4:24 pm
Hi, great tutorial, but I have a question:
I added the fruit Lemon and as a description I retrieve the content of an internet page. But how can I retrieve the content of a local txt or rtf file instead of an internet URL?
Can’t really figure how to do it…
Thanks
Fruit *watermelon = [[Fruit alloc] initWithName:@”Watermelon” description:@”My favorite flavor of Jolly Ranchers”];
Fruit *lemon = [[Fruit alloc] initWithName:@”Lemon” description: [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.icaramel.com"]]];
Kevin Says:
February 6th, 2009 at 8:58 pm
@luiz
How did you connect the UIImageView in interface builder? My FruitViewController nib doesn’t show a “picture” outlet that I can connect to the UIImageView. When I add one by hand, the app crashes when a row is selected.
Help!
Julien Says:
February 7th, 2009 at 9:40 pm
hey Brandon,
First I just wanna say thanks so much for the epically awesome tutorials.
Well I just have one question, say I wanted the 2nd view to display formatted web based content, what would I do in this situation?
Thanks
fskhalsa Says:
February 9th, 2009 at 2:07 am
Sweet. Nice tutorial!
I just spent an hour doing this, and I’m still deciding whether it’s fully fulfilling or not to see an app that does as little as this after all that. I’m sure that’s just the first time though, and if I knew what I was doing, it would take only 15 minutes.
Either way, even though I don’t understand most of it, I’m still learning a lot, so Thanks!
Keep writing tutorials!
fskhalsa Says:
February 9th, 2009 at 3:25 am
I have one question Brandon. I’m trying to add pictures of fruit to my app. I followed all your instructions to Luiz, and added the image view and connected it. Each fruit has it’s own image assigned like this: picture:@”apple.jpg” etc. The problem is that I don’t know where to put the pictures so it can find them. The app runs, just there is a blank space where the pic should be. I’ve tried putting them in the project directory, and tried dragging them into xcode. I’m a total noob at this, so there is obviously a place for them, I just don’t know where. When I dragged them into xcode, i could select one of them in the attributes field for the uiimageview in ib, but then that image would appear for all the fruits. I want it to pick each image that I assigned differently for each fruit. Any help?
Thanks a ton.
Drudoo Says:
February 9th, 2009 at 9:32 am
Hey
Really nice tutorial, but i still cant figure out how to change the text to a picture. I tried everything, from the comments.
Can someone send me there project file(the one that got it to work) or can anyone explain it to me.
I really new i programming, so any help would be great
You can email me at: vanggaard26@hotmail.com
Kevin Says:
February 11th, 2009 at 9:31 am
@fskhalsa
You just have to drop them into your Resources folder. A dialog box will pop up asking you to add it to the project. Make sure you check off the “Copy” box.
I also want to add images. I followed the instructions for theluiz too, but mine doesn’t work. I don’t see how to connect the UIImageView in IB when the First Responder doesn’t have an outlet.
Any help?
Jonathan Says:
February 12th, 2009 at 2:46 am
Thanks for the tutorial… Just starting on iphone dev after a few years without coding, and you’re of great help!
eXDee Says:
February 12th, 2009 at 7:33 am
ok, I have the first tableview set up with an array of different choices to choose from. I have the interface for the second view set up, which is also a tableview. But what Im having trouble with, is inputting another array into the second view.
I want for there to be a different array for each cell that is chosen in the first view. Any idea how I could get that done?
it would be greatly appreciated.
thanks
Kevin Says:
February 12th, 2009 at 9:35 pm
@Stephen James
Looking at your iGreyhound app it would appear you figured out how to place the text and an image on the same view. How did you get the images to appear for the selected row? We’re all trying to figure that one out.
Tim Says:
February 13th, 2009 at 1:02 am
Excellent tutorial… I got everything working as your laid out and so I decided to stretch things a little and I have a couple questions.
First, I changed out the TextView (UITextView) to be a Scroll View (UIScrollView) and made the changes to the IBOutlet and @property statements accordingly and then went over to Interface Builder and made all the connections. (all looks good)
Everything compiled (with 1 warning) and the app fired up in the simulator it dumped. I am getting the warning that UIScrollView may not respond to -setText in the RootViewController.m file. This line [self.fruitView.fruitDescription setText:[fruit description]]; is where the problem is.
In a nutshell I understand that setText isn’t going to work with UIScrollView, but I dont know where to go to figure out what will. I have read everything that I can get my hands on and I guess I am just missing it.
Can you point me in the right direction??
Also, assuming I get past this issue, in my example I have a lot more text that I want to put in the ScrollView or TextView then what you did in the tutorial and I was wondering if there was a way to force a line break in the text inside the description value that we set inside the FruitAppDelegate.m file? I was hoping to be able to bold or italic a specific word, but it appears that is not possible in TextView on an individual word basis, only on the entire text. If the answer is no to both TextView and ScrollView, what are my options to do this?
Thank you very much! Excellent Tutorial.
Tim
Kevin Says:
February 13th, 2009 at 2:12 pm
Still trying to load in the images of the fruit. Why does this not work?
in RootViewController.m
//set name in the nav bar
self.fruitView.title = [fruit name];
//put description into the label on the view
[self.fruitView.fruitDescription setText:[fruit description]];
//put the image into the UIImageView
self.fruitView.picture = [fruit picture];
It build but crashes when I select a row. Maybe I need a line similar to the “description” one, although I don’t know how to do it.
Please help. The name shows in the nav bar, the description shows on the label, but no image.
twitter @RyeMAC3
Mark H. Delfs Says:
February 15th, 2009 at 10:41 am
Another great tutorial! For future tutorials I would love to see more stuff about e-mailing results, location services, and things like that. As usual, LOVE these tutorials and please keep them up! I would buy a book of these in a heartbeat!
reetu.raj Says:
February 16th, 2009 at 2:45 am
hi there @brewersfan,
for changing font size for table rows text I have added following line
cell.font = [UIFont boldSystemFontOfSize:9];
in given below function:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell…
cell.text = [specifics_menu objectAtIndex:indexPath.row];
cell.font = [UIFont boldSystemFontOfSize:9];
return cell;
}
Jinu C Joseph Says:
February 17th, 2009 at 7:10 am
are the ringtone, wallpaper settings manageable/accessible from an app? If yes, how we can do this using the SDK. Thanks in advance.
Kevin Says:
February 20th, 2009 at 3:02 pm
Twittered with Brandon this morning…
…for those looking to solve the “load an image into the FruitViewContorller.xib” thing…
…he says to “in viewDidLoad on fruitViewController do self.imageView.image =fruit.image and it will display the correct image for the fruit”
I can’t wait to try this when I get home!
@RyeMAC3
Kevin Says:
February 23rd, 2009 at 3:12 pm
@Brandon
I’m so discouraged…
“self.imageView.image =fruit.image” didn’t work.
I tried “self.fruitView.fruitPicture = [fruit picture];”
It builds OK, but crashes when I select a row.
What I don’t understand is why “[self.fruitView.fruitDescription setText = [fruit description]];” works and “self.fruitView.fruitPicture = [fruit picture];” doesn’t. I am putting this in RootViewController.m along with “[self.fruitView.fruitDescription setText = [fruit description]];” since that is where the “fruitView” is talked about. I tried to put it in the FruitViewController, but it doesn’t build there. It complains about not knowing what “fruitView” is.
Kevin / @RyeMAC3
Mozo Says:
March 7th, 2009 at 6:47 pm
@wmparry I was just experiencing the same problem as you described… I couldn’t drag a connection from the fruitDescription outlet to the UITextView in my view. I found that in my case, this was because I had defined the @property incorrectly in FruitViewController.h.
I had:
@property(nonatomic,retain) IBOutlet UIText *fruitDescription;
rather than:
@property(nonatomic,retain) IBOutlet UITextView *fruitDescription;
Once I corrected UIText to UITextView I was able to make the connection.
Brian Sterling Says:
March 30th, 2009 at 7:21 pm
Here’s how I solved the issue of the new .xib files not going into the project’s directory by default.
Rather than starting Interface Builder by double-clicking any of the existing .xib files, do File:New File… directly from Xcode. Choose User Interfaces from the list on the left, then the desired template. Once you save, it will automatically be in your project directory.
This works for me with Xcode 3.1.2.
welshbaloney Says:
April 5th, 2009 at 8:20 am
Excellent tutorials. I’m a beginner and find that I follow along with the more complicated ones (like this) somewhat parrot-fashion. I.e., I can’t visualize what’s going on so I just copy the code.
I’m learning from the code itself, but what would totally complete the learning experience would be a diagram, such as a design pattern diagram that shows each main object, controller, view, etc. their methods and the messages between them.
Then I would better understand the WHY as well as the HOW.
I realize you’re doing this on your own time, and thanks very much for that.
Tony.
welshbaloney Says:
April 5th, 2009 at 9:26 am
One other thing…I’m still getting confused over when/how to use a delegate vs. a controller. I get that (in this project) there’s a RootViewController that pairs with the MainWindow.xib and a FruitViewController that pairs with a FruitViewController.xib. But then that FruitAppDelegate pops in.
I don’t understand why the fruits array (e.g.) was defined there instead of somewhere else….
Am I making sense and can anyone just help clear this up for me?
Thanks
sant Says:
April 8th, 2009 at 10:54 am
Hi Brandon,
Great tutorial. One question.
Once the user clicks on fruit -> apple, how do i make the text in “apple” screen uneditable. I want a simple appl where the user clicks on one of fruits and reaches the fruit description which is basically read only.
Any answers to that ?
Thanks
sant Says:
April 8th, 2009 at 10:55 am
Hi Brandon,
Great tutorial. One question.
Once the user clicks on fruit -> apple, how do i make the text in “apple” screen uneditable. I want a simple app where the user clicks on one of fruits and reaches the fruit description which is basically read only.
Any answers to that ?
Thanks
surfous Says:
April 12th, 2009 at 5:29 pm
@sant
You need to make the UITextView in the FruitViewController nib not editable.
To do this, double click FruitViewController.xib to open it in the Interface Builder. From the window titled FruitViewController, click to select the TextView item (you may need to twist down the View item to see it). Alternately, you can click the UITextView in the interface window (it has the “Lorem ipsum” text in it).
Open the atributes inspector (command-1) on the TextView. the inspector palette window should have the title “Text View Attributes.” In the very top section you should see Alignment, Text color and a checkbox named “Editable.” Uncheck this box, save & quit the interface builder.
Finally, go back to Xcode and rebuild and run the code. The fruit descriptions should no longer be editable.
rbcrane Says:
April 19th, 2009 at 1:34 am
Thanks for the great tutorials Brandon. I’ve been coding since the 80s, it’s been a while since I hacked UIs. These tutorials are helping it all make sense.
SomethingSpecial Says:
April 20th, 2009 at 8:41 pm
Nice screenshots you stupid f u c k i n g a s s h o l e.
Ryan Says:
April 27th, 2009 at 9:00 pm
Hi Brandon (or anyone who can help),
There is a simple problem I have had for days. I need to get an NSArray created in one view controller passed into another one. The array is created when a user presses a button and then it gets placed into a UITableView using the other view controller.
Since the array is always determined at real time, I cannot set it in myAppDelegate and therefore can’t use the appDelegate method,
myAppDelegate *appDelegate=(myAppDelegate *)
[[UIApplication sharedApplication] delegate]
used to create an instance of the array for the table view controller like in this tutorial. Any ideas? Thanks!
ashwanik04 Says:
April 29th, 2009 at 2:30 am
Hi Ryan,
Regarding your post, You can create a property in the child view and give it value when u r drilling to child view from parent view in the didSelectRowAtIndexPath method of the parent view.
Hope it may help u.
Kawititnow Says:
April 30th, 2009 at 1:00 pm
Hey Brandon, and everyone else,
This has been a great tutorial to get my feet wet with the iPhone SDK.
I’m trying to expand a little bit on the app, and what I’m trying to do is make it so the user can add and delete fruits.
So far I’ve added an addButton to the RootViewController. When the addButton is pressed a NewFruitViewController is “popped” up, which contains 2 text fields (1 for newFruitName and the other for newFruitDescription). I had implemented a Save button at the top of the navigation bar, but when I click it I “think” it has added the new fruit to the fruits array, but it does not show up in the RootViewController.
Any ideas on how to insert the newFruit data into the fruits array and display the new fruit as a new cell???
Thanks in advance
Kawititnow Says:
April 30th, 2009 at 6:55 pm
below is my code for NewFruitViewController.m with the save method:
#import “Fruit.h”
#import “NewFruitViewController.h”
#import “FruitAppDelegate.h”
#import “RootViewController.h”
- (void)save:sender {
NSString *name = newFruitName.text;
NSString *desc = newFruitDescription.text;
Fruit *newFruit = [[Fruit alloc] initWithName:name description:desc];
FruitAppDelegate *delegate = (FruitAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.fruits addObject:newFruit];
[newFruit release];
[self dismissModalViewControllerAnimated:YES];
}
My thinking was that if I can add the object to the array then the rest would take care of itself. Any help would be appreciated.
ashwanik04 Says:
April 30th, 2009 at 10:42 pm
Hello Kawititnow:
Instead of using
[self dismissModalViewControllerAnimated:YES];
use
[self.navigationController popViewControllerAnimated:YES];
It will work
Kawititnow Says:
May 1st, 2009 at 6:39 am
ash
I tried that and when I went to hit the save button nothing happened. The only way I could get out of the modalViewController was if I hit the Cancel button, which uses the dismissModalViewController…
So it seems it either isn’t adding the newObject to the array, or it isn’t reloading the table?!?
ashwanik Says:
May 1st, 2009 at 9:28 am
@Kawititnow
I modified the fruit application code provided here with the requirement u said, it did work for me, if u can give me your email I can give the code, Of course with your permission onle
Chito Says:
May 1st, 2009 at 5:34 pm
Great tutorial!!!!!! VERY useful.
I got everything to work. How do I get it so that the user can go back to main screen after they go to the fruit description. On the top it simply lists the name of the fruit that is being described.
Chito Says:
May 1st, 2009 at 9:20 pm
At the fruit description window it does go back to fruits when you click on the left side of the top bar on the window but there is no button present.
Mick Says:
May 15th, 2009 at 10:52 am
I cannot get this tutorial to work. Two issues I’ve noticed. First, when I create the NSobject subclass Fruit, the file Fruit.h does not contain “#import ” but “#import ” instead. The import UIKit is found in the RootViewController.h file.
Second, even if I put the import UIKit line in the Fruit.h file, it does not compile. I get the same “‘error: request for member “fruitDescription” in something not a structure or union’” error that Dezorian mentioned above.
Help!
Mick Says:
May 15th, 2009 at 10:58 am
My last post came out wrong - it was suppoed to say that the Fruit.h file did not have the import UIkit line, but had import foundation.h instead…
Julius Says:
May 17th, 2009 at 1:28 pm
Regarding wmparry’s question about not being able to get the fruitDescription outlet attached to the TextView; check your FruitViewController.h file and make sure you’ve declared the fruitDescription property as a UITextView and not a UIText (if you’re like me you might have hit Tab to fill in the property type and not noticed that it doesn’t complete the full typename of UITextView.
sjalfr Says:
May 18th, 2009 at 9:12 am
Hi,
Worked like a charm, thanks. But I was wondering… When in the “description” view, if I click on the actual description, the keyboard pops up and there is no way to make it disappear!
Going back to the fruit list and clicking on another fruit brings me back to the description as it should, but the keyboard is still out even without clicking on the description again.
Any ideas on how to get rid of the keyboard, or better yet, not having it pop out if you don’t want the list to be editable.
Thanks
Chito Says:
May 18th, 2009 at 9:40 pm
Thanks again for the tutorial. Worked great. I have purchased and Objective C book and a Iphone app book but you do a better job of explaining. If you have the time, can you please help with creating this same app but to have one more layer.
First view( List of foods”vegetables,fruits,sandwiches,etc.”
Next view(List of items in the categories of foods “Fruits-strawberries,pineapple,lemons,etc.”
Last View( The description of each of those fruits).
Please Help.
Todd Says:
May 25th, 2009 at 10:56 am
From what I can tell there is going to be a memory leak in the app since the fruit objects are not being deallocated.
Each [fruit alloc] needs to be released.
Anon Says:
June 2nd, 2009 at 10:52 pm
Doesn’t anyone do anything in Obj-C without using IB? I, personally, greatly dislike using any form of graphic interface for programming. Maybe it’s an old habit from C and C++ but I don’t the idea of having a single line of code in my program that I can’t explain entirely.
Does anyone know of any anti-IB tutorials out there?
Mike Says:
June 9th, 2009 at 10:44 am
Is it possible to use this tutorial as a base to making editable text views (changing the description) which save using NSUserDefaults, or will it reset every time because the description values are predefined. If it is possible can anyone post some code that might put me on the right track?
BRUCE Says:
June 16th, 2009 at 1:53 am
I got 21 errors by following your tutorial as it is.
i am using xcode 3.1.2 is it some version problem bcoz
when i load nsobject class i dont get
header by default,, if i include it gives //no such header exists
also in each function i get error like eg: for viewcontroller
//undeclared in this function first to use
BRUCE Says:
June 16th, 2009 at 6:44 am
Thankx For tutorial plz solve the error in ur code:
i got error ‘ fruitdescription is not something like structure or function ‘ in RootViewController
FruitViewController import statment in Rootviewcontroller is
showing error like ‘ not found in this directory ‘
Eric Says:
June 18th, 2009 at 11:42 am
Hi Brandon,
Thank you for your generosity and sharing your knowledge with us. I’m new at programming and find your tutorials very useful!
I have a problem creating the NSObject subclass file. The only templates I have when I click File - New File are Objective-C class, Objective-C test case class, and UIViewController subclass.
I just installed version 3.1.3 of xCode. Am I missing something? Thanks for your help
Eric
Viswanath Says:
June 19th, 2009 at 8:58 am
Hi,
i’m newbie…
I could not able to see the view. its just blackout on my simulator, but no errors as well.. i just did as what told above.. any idea or suggestions
Eric Says:
June 19th, 2009 at 10:03 am
Regarding my previous post - in xCode 3.1.3, you need to choose the Objective-C class template and choose NSObject from the «Subclass of» drop-down menu, and then remove the header and replace it with the header.
Everything worked very well.
stevebillgates Says:
June 21st, 2009 at 9:27 pm
I am seeing the same as what Vishwanath mentioned…its a blackout screen…and also I see the app crashing
Viswanath Says:
June 22nd, 2009 at 1:31 am
Hi
I was looking into the log file and it say UIKit not found. .
any help!
Viswa
John Costa Says:
June 23rd, 2009 at 8:47 pm
Hey! Can you make a tutorial on how to add a search. If you already have, I’m sorry. I’m new to the site. It would also be VERY useful to add a search to the site! lol
J Mill Says:
June 24th, 2009 at 9:52 am
I was just wondering if its possible to put images on the table instead of the fruit
Graham Says:
June 26th, 2009 at 11:53 am
Hmmm…I keep getting an error after the applicationDidFinishLaunching portion. It’s for the three variables of type fruit (*apple, *orance, ect.). Keeps saying: error:syntax error before ‘objc_string’ token. Not sure why it’s doing this…looks like the rest of the code is compiling fine…
Ainn Says:
June 28th, 2009 at 3:19 pm
Hey, very nice tutorials.
Is there a way to connect this tutorial with the game tutorial? Basically, if you press a button it starts the game up?
Thanks
Matt Says:
July 3rd, 2009 at 5:27 am
Just want to say a big thankyou, just got a macbook for the purpose of making apps and found it so difficult, now I have gone from nothing to completely understanding everything youve explained and adapting it for my own app, going to continue with the tutorials now ^^ thanks again!!




























