Navigation based Application in Xcode does not auto generate window and navigationController - iphone

In the Apple sample code, the AppDelegate contains window and navigationController as instance variable. (http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Listings/1_SimpleTableView_Classes_SimpleTableViewAppDelegate_h.html%23//apple_ref/doc/uid/DTS40007318-1_SimpleTableView_Classes_SimpleTableViewAppDelegate_h-DontLinkElementID_5)
#interface SimpleTableViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
The code generated by XCode4 is only as simple as
#interface SimpleTableViewAppDelegate : NSObject <UIApplicationDelegate> {
}
Why the two instances variables can be missed?
Why only the *window need to be declared as IBOutlet, but not *navigationController?
Thanks.

The LLVM compiler used by Xcode 4 can automatically generate instance variables for synthesized properties. When a property is declared and synthesized, the compiler will automatically generate the corresponding ivar. It's a great feature as it not only saves typing but also makes the external interface of a class cleaner.

Related

What is the owner of UI elements of a view controller? iOS5 with ARC

I am new to iOS 5. From Apple's documentation I know what ARC is and "Owner of an object should using strong notation." After read "Hello World" , I noticed a strange thing.(I mean that was confusing me)
HelloWorldAppDelegate:
#interface HelloWorldAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
HelloWorldViewController:
#import <UIKit/UIKit.h>
#interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)changeGreeting:(id)sender;
#property (copy, nonatomic) NSString *userName;
#end
Here:
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
UI elements have weak notation and none of file have a strong references to them. So I am confusing what/who hold them?
In my opinion you can consider the NIB/XIB as the owner of those objects. None of your classes own them. See "Managing the Lifetimes of Objects from Nib Files" in Resource Programming Guide:
From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File's Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should will therefore typically be weak by default, because:
Outlets that you create to, for example, subviews of a view controller's view or a window controller's window, are arbitrary references between objects that do not imply ownership.
The strong outlets are frequently specified by framework classes (for example, UIViewController's view outlet, or NSWindowController's window outlet).

How to call nsobject class on a button in view controller

i am doing an apps which i need to call nsobject class in a view controller class, i try a couple method but its not working either i get a sigabrt error or the apps crash.
please help me.
I'm lost for days now.
any one have a sample code for calling nsobject from view controller?
thanks in advance for you kind help.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
-(IBAction)firstButton;
#end
#import <UIKit/UIKit.h>
#interface TutoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
UIButton *firstButton;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIButton *firstButton;
#end
Please post some code where we can provide the correct solution. Even I don't get problem exactly but if you want to call class object then do the following.
-(void)buttoevent:(id)sender
{
nsobjectclass *obj = [[nsobjectclass alloc]init];
//if you want to call any method of that class then do the following.
[obj methodname];
}
First of all check if you have imported the class before using it

When declaring class properties/variables, can you just declare it via #property?

I've noticed that some generated classes only declare class properties/variables via #property, and don't include them within the #interface, as such:
#interface AddItemViewController : UITableViewController {
}
#property (nonatomic, retain) UITextField *itemName;
I was just curious if that's an acceptable way to do it, or if that is done for different reasons?
I normally do this:
#interface AddItemViewController : UITableViewController {
UITextField *itemName;
}
#property (nonatomic, retain) UITextField *itemName;
I declare it first in the #interface and then add the #property for it...
* Update *
I just wanted to update this a bit, because it's still not 100% clear to me.
I always thought that to declare a #property, you first needed to declare it within the #interface first, and then I saw this:
#interface mInventoryAppDelegate : NSObject <UIApplicationDelegate> {
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
All of those #property declarations are declared only as #properties, and not within the #interface.
For example, if I had say NSString *myString - I can declare that in the #interface and not as a #property and still have access to it no problem, but the getters and setters won't be created. I could also declare it in both. But what if I just declare it as #property, as such:
#interface AddItemViewController : UITableViewController {
}
#property (nonatomic, retain) NSString *myString;
Notice how I didn't add it between the #interface { } - how does it differ.
Sorry for repeating, but I'm just trying to reword this so that I can get an answer that makes more sense to me.
With the "modern" runtime, which the iPhone uses, the compilers can create the instance variable for you. Just use:
#synthesize itemName;
or if you prefer...
#synthesize itemName=_itemName;
...in your implementation. The compilers will then create ivar 'itemName' or '_itemName'.
This is of course for the case that the property is a simple getter/setter for one particular instance variable.
EDIT: NVM, per #bbum, what I thought of in my mind as the "32-bit" sim is actually the older simulator that didn't behave like the new runtime. The newer simulator is still 32-bit, and supports this behavior. See his comment below.
update
In response to your updated question:
The "interface" for a class is everything up to the #end. I think what you are calling "interface" is actually just the instance variables within the {}. What is between the {} are the instance variables for your class. The whole #interface includes those instance variables PLUS the method and #property declarations between the {} and the #end.
So I think what you are really asking is if you have a #property in your #interface, and that #property is just a simple getter/setter pair, then do you need to declare a "backing" instance variable also in your #interface, within the {}.
The answer for iPhone is NO. The compilers (both) can create that instance variable for you.
I hope that answers the question?
It is perfectly acceptable to do it this way. You would however need to implement the setter/getter methods yourself. These can not be created using the #synthesize syntax.
One reason to use this approach could be to have the properties based on something more complex than just setting and getting a value. It doesn't however make much sense for simple Nib connections as in your example.

Adding Record using Core Data with Tab Bar Controller

I am trying to add a record to the database using core data. The appDelegate has the managed object model, context, and store coordinator setup in it. When the app is launched and I query the fetchResultsController method in one of my views the database is created matching the scheme with the correct table names and columns in it. However the problem comes when I try to add a record to the table.
The BurpListNavController.h file has the following contents (I am just learning):
#import <UIKit/UIKit.h>
#class BurpRecordController;
#interface BurpListNavController : UINavigationController <NSFetchedResultsControllerDelegate> {
BurpRecordController *burpRecordController;
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
}
#property (nonatomic, retain) IBOutlet BurpRecordController *burpRecordController;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- (void)saveBurpLocal:(id)sender;
#end
I then have a view that records the burp, yes another one of the thousands of burp applications. haha. The following code is as follows:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#class BurpLocal;
#class BurpListNavController;
#interface BurpRecordController : UIViewController <AVAudioRecorderDelegate, UIActionSheetDelegate> {
/*** Outlets to talk to the view ***/
IBOutlet UITextField *burpName;
IBOutlet UIButton *_recordButton;
/*** Standard Variables ***/
NSURL *recordedTmpFile;
//AVAudioRecorder *recorder;
NSError *error;
BurpLocal *burpLocal;
BurpListNavController *burpListNavController;
}
/*** Properties ***/
#property (nonatomic, retain) IBOutlet UITextField *burpName;
#property (nonatomic, retain) IBOutlet UIButton *recordButton;
#property (nonatomic, assign) IBOutlet BurpListNavController *burpListNavController;
#property(nonatomic, retain) BurpLocal *burpLocal;
/*** Method ***/
-(IBAction)saveRecording:(id)sender;
-(void)applicationWillTerminate:(NSNotification *)notification;
#end
When the end-user pushes the "save" button it calls the "saveRecording" method which I can step into and is great. Then I try to call the following line of code within the "saveRecording" method: [burpListNavController saveBurpLocal:sender]; thinking this will call the "saveBurpLocal" method in the nav controller and it just steps over it, does not stop at the break point in the nav controller method and then just goes to the end of the current "saveRecording" function. Does not write a record to the database or anything.
Please help! This is driving me crazy.
It's difficult to tell without more code; but it sounds like burpListNavController has not been set. When you step through the saveRecording: method, is the value of burpListNavController 0x0? If it is then you have forgotten to set that iVar somewhere.
If you have come from another language this might sound a little strange, as usually calling a method on a null pointer you cause you to crash. This isn't the case in Objective-C though, it is perfectly legal to send a message to nil; but don't expect anything to actually happen.

self "not a structure or a union" in a UIViewController class. Why?

I have spent a lot of time trying to figure out what the problem is... but with no success.
My code is the following:
ResultGoalViewController *resultGoalViewController = [[ResultGoalViewController alloc] initWithNibName:#"ResultGoalViewController" bundle:nil];
[self.goalNavigationController pushViewController:resultGoalViewController animated:YES];
I get the error:
"error:Request for member 'goalNavigationController' in something not a structure or a union."
My class is UIViewController.
goalNavigationController is a Navigation Controller (defined within a Tab Controller).
What am I missing?
goalNavigationController should be a property or an accessor in your UIViewController subclass.
It sounds like self doesn't have a goalNavigationController property. If it does, you should post where it's declared so we can see that.
Thanks Chuck, thanks Mipadi.
I have a goalNavigationController property in the delegate application.
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *mytabBarController;
NavigationGoalViewController *goalNavigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) NavigationGoalViewController *goalNavigationController;
#end
But it's not in my current UIViewController Class, as my goalNavigationController was first built/called within my delegate application. I am now calling it from the ViewController of the first view of my Navigation (I am trying to load the second view of my navigation controller).