Main AppDelegate - ios5

Sorry for questioning so dummy question, but I swear I used Google before doing it :)
So, the question is - how do I set the main AppDelegate class for my application? I mean, if I don't want to use the one generated by Xcode but to work with my own class for that.
I'm newbie in iOS programming, so probably I misunderstood some part of theory.
I will highly appreciate any of your help!

I can't imagine why you would want to reset the app delegate that your template sets up for you, but to it you'd take a .h interface and have it conform to the <UIApplicationDelegate> protocol, alloc & init that object (for the sake of arguments, we'd call it yourNewlyCreatedDelegateInstance) and then you could set your app delegate to that via [[UIApplication sharedApplication] setDelegate: yourNewlyCreatedDelegateInstance].

Related

iOS Dev via Xcode: UIView & UITextView/UIPickerView - Cannot Assign Delegate?

Up until updating Xcode last night, this was working great (though probably through sheer dumb luck!).
I'm getting an error now for my UITextViews, Fields and UIPickerViews when assigning the delegate:
[m_textField setDelegate:GAMESTATE->glView.self];
m_textField is defined as UITextField*. glView is defined as a UIView*. To keep things simple, I'm using just one UIView for the entire app (could also be a problem). I only need access to a couple of basic operations in the app to get simple text info from the user. Now I get the error:
Cannot initialize a parameter of type 'id<UITextFieldDelegate>' with an rvalue of type 'UIView*'
Like I said, I probably was doing something wrong in the first place, and it only worked through sheer, dumb luck! If it helps, I'm already subclassing my UIView as an Accelerometer Delegate, as such:
#interface GLView : UIView <UIAccelerometerDelegate>
Any help, suggestions or tips would be greatly appreciated. I've stumbled through some books this morning and brushed up on several Apple docs, but a quick work-around doesn't seem possible. I'm not adverse to refactoring the way I take input from the user, I could just use a hand getting there.
You should create, for example, NSObject that will delegate
for UITextView : UITextViewDelegate.
for UIPickerView : UIPickerViewDelegate
Your class declaration should look like this:
#interface MyViewDelegator:NSObject<UITextViewDelegate, UIPickerViewDelegate>
Then you should alloc+init it and set it as delegator of your text/picker views:
MyViewDelegator *delegator = [[MyViewDelegator alloc] init];
[m_textField setDelegate:delegator];
Don't forget to implement appropriate methods in MyViewDelegator that are not optional for protocols.

NSObject ModalView and Delegate

I'm modifying someone else's code and trying to use a ViewController however it is currently an NSObject. So below I have added newDelegate
#interface myAppDelegate : NSObject <UITableviewDelegate, newDelegate>
in my code I try to bringup a modalview with
[self presentModalViewController:newModalView animated:YES];
I get the error message 'myAppDelegate' may not respond to '-presentModalViewController:animated:' that's fair enough, it is an NSObject. Can someone help me with a possible approach?
If someone wrote code with an NSObject as a UIViewController, you've got a lot of problems. "Modifying someone else's code" means to me "I'm the new vendor after the old guy screwed it up so bad by lying to the client and SAYING they could do iPhone dev work". Maybe go back to the client and say "we need to redevelop from scratch, this is garbage"? :)
As for the issue at hand - app delegates ARE NSObjects -- so that part looks right. However, an app delegate should not usually be a UITableViewDelegate. You'd have a window property and a rootViewController property in the app delegate, and the final call of appDidFinishLaunchingWithOptions: would instantiate the view controller and load it into the window. That view controller would then handle any views (and likely be the UITableViewDelegate in this case).
The presentModalViewController: code is built-in to UIViewController, so you're not going to be able to it to work directly from the app delegate.

How do you deal with singleton objects in your app?

The first approach that comes to my mind is to put the singleton object in the appDelegate object as property. In this way you can access it from anywhere using
#import "myAppDelegate.h"
// ...
[[(myAppDelegate *)[UIApplication sharedApplication] delegate] SingletonObj]
The downside is you have to explicitly cast and import the header of your delegate to tell the class you're working with that SingletonObj is actually a property of the delegate. And I think this makes the code smell a little.
The second approach is to create a legit singleton class. This, however, require more work. And I personally think that one Singleton class, is more than enough.
I'm not a programmer so I would greatly appreciate corrections on my reasoning, and opinions on the subject.
Singletons are most often handled in a class via a method like +sharedInstance.
Here is a good write-up.
I found a xcode template for easy singleton generation on the net.
Best practice is to only ever put UIApplicationDelegate-related things in your AppDelegate class. Evarr.
Decide if you really need a singleton. If you do, just make a legit singleton class. It will be easier in the long run; did you notice how long it took to type that monster of a line, where you tried to get the singleton object out of the AppDelegate? Agh.
(Also, just a little bit of idiom: classes start with a capital letter, and methods start with a lowercase letter; hence, you problem meant [[(MyAppDelegate *)[UIApplication sharedApplication] delegate] singletonObj].)

what is applicationDelegate - iPhone

(WebService_1AppDelegate*)[[UIApplication sharedApplication] delegate]
here - in above statement "WebService_1AppDelegate" is my application name.
But i don't understand what does this statement mean.
Please explain me - briefly this statement.
I will be thankful.
Thanks in advance for helping me.
An AppDelegate is a singleton that every iPhone program has. This class is responsible for receiving many system calls such as applicationDidFinishLaunching, applicationWillTerminate etc. It is the entry point for an iPhone app.
Each app defines their own AppDelegate and thus WebService_1AppDelegate is yours. You can define your own methods/properties there and when required cast the AppDelegate returned from [UIApplication sharedApplication] to access your methods/properties.
WebService_1AppDelegat *appDelegate = (WebService_1AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate yourCustomMethod];
That statement only make sense if you have a class named WebService_1AppDelegate. Then it's casting the return value of that method (which is the application delegate) to that type. If that is indeed the name of your application delegate class then that cast is superfulous,
Are you trying to figure out what "AppDelegate" is in an iPhone app? If so, AppDelegate acts as a central storage medium for your application that allows you to share data across all of your controllers seamlessly. If you're familiar with web development it is just like a Session variable.

Best Application Delegate Practice

I have been making a few apps here and there, and I know my way around. What has always confused me is accessing global attributes, and where the best place to set them are. I have a few questions about accessing things and how to access them.
Do you have to include your application delegates header file into any other other file you want to access it in? Say I have a view controller, that I would like to use, do I need to include the .h inside my view controller's .h? Or can I set the:
#class AppDelegate;
Can you only access the delegate by typing out:
[UIApplication sharedApplication].delegate
EACH and every time? Is that something I just have to get used to? Or could I set the following in my implementation in each .h:
AppDelegate *delegate;
And inside the init function, put the singleton instance to that variable?
Sorry if this was off structure, but I think it's a logical question people have a problem encountering and solving.
Maybe you need to reconsider how you are using the App Delegate? It sounds to me like perhaps you are not making a very good class design.
Regardless, here's a way to make it easy. Don't put this in init just use it when you need it.
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Naturally, replace MyAppDelegate with the actual class name of your app delegate.
Another possibility is to add the code to use a properly casted app delegate reference as a #define in the app delegate header file, so after including it you can do something like:
MYAPPDELEGATE.customProperty = blah;
However I tend to favor just writing out the line that John presented, as use of #defines confuses code completion which I find more annoying than just typing the line.
As also mentioned, if you have a ton of references to the app delegate you may want to restructure to keep some of those references closer to home.