Assigning ViewController to a delegate, Is it good? - iphone

I am newbie in iOS programming, Recently I came across a tutorial where author assigned a ViewController to a textField delegate. Is it good to do this? As Xcode is giving me warning.
discussIDTextField.delegate = self;
self is DiscussViewController and above code is inside DiscussViewController.m
Code works fine but I don't like yellow bubbles showing on my screen while writing codes. If I want to get rid of this warning what should I do?
Warning : Assigning id from incompatible type
'DiscussViewController'.

The view controller (self) has to implement the UITextFieldDelegate protocol. So your #interface definition should look something like this:
#interface DiscussViewController : UIViewController <UITextViewDelegate>
And then of course in the implementation implement some of the delegated protocol methods.

In your .h file add UITextFieldDelegate between < > so that your view controller becomes text field delegate and then in the .m file implement delegate methods that you need.
#interface DiscussViewController : UIViewController <UITextFieldDelegate>

Related

about custom UItextField on iphone including delegates

actually i want to create custom class for textfield and in that same class handling of all things like delegate,any other properties also.from controller only i need to call just a single method of that class.
You mean using the textfield delegate in your own class so you can use the Textfield methods?
Try adding < UITextFieldDelegate > in your .h file like:
#interface YourClass : NSObject <UITextFieldDelegate>
Hope this is what you're looking for.

programmatically create UITableViewController IOS 5

When starting a new project (single view based), I began to get a strange crash when attempting to use a custom UITableViewController. The custom UITableViewController works just fine in other application started a while back, but now it seems to fail when used in new projects.
This is how I am instantiating the UITableViewController:
TestViewController * tableViewController = [[TestViewController alloc] initWithStyle:UITableViewStyleGrouped];
[self.view addSubview:tableViewController.tableView];
The compiler is a lot more strict now, so I make sure to add the required protocols in the headers as follows:
#interface TestViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
I get the following message:
-[__NSMallocBlock__ numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x654dcd0
In the implementation for the custom UITableViewController, I am hard coding the number of section and rows, but I am still getting this problem.
I don't know where to go from here, I appreciate any help I can get,
Thanks

iPhone - UIImagePickerControllerDelegate inheritance

I have added a UIImagePickerController to a UIViewController. I have also assigned the UIImagePickerControllerDelegate to that UIViewController.
When I execute the following line,
myPicker.delegate = self;
Xcode gifts me with the following message:
warning: assigning to
id
from incompatible type 'RootViewController'
Then I added the UINavigationControllerDelegate protocol to the same UIViewController and the error message vanished.
So, do I have to add both protocols to the UIViewController when I add a UIImagePickerController?
If the UIImagePickerController is a subclass of UINavigationController as stated in the docs, shouldn't this be automatic? Why do I have to add its parent's delegate protocol and not just the UIImagePickerControllerDelegate protocol?
Is this a bug or am I missing something?
As you noted, UIImagePickerController inherits from UINavigationController. It uses the same delegate property though and doesn't declare a (hypothetical) "imagePickerDelegate" of its own, so your delegate has to conform to both protocols. It makes sense, because you're also assigning the same delegate to the UINavigationController part (that knows nothing about the image picker).
The API design is a bit questionable here in my opinion, but anyway, all methods in UINavigationControllerDelegate are optional, so it suffices to declare that you conform to the protocol and be done with it.
Add these code like below,you can see the warning disappear.
#interface viewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> { }
#end
The Protocol of UIImagePickerController and UINavigationController must be added in your interface, this can make the warning invisible.

Possible to use UIActionSheet from Application Delegate?

I have a common UIActionSheet which I use across about 10 different view/view controllers. I'm wondering if it's possible to use UIActionSheet from the app delegate in order to prevent code duplication?
So far my attempts to use an action sheet from the delegate haven't worked, I suspect my problem lies when calling the showInView method - do I need to instantiate an object of my view controller then use viewController.view here? If so how can I then tell which view called the action sheet method from the delegate?
I didn't try the approach proposed by c_phlat, but I wonder to what self.view is mapped.
I did manage to do it like this:
[actionSheet showInView:window];
it works just as well.
I was having the same problem, and I recently figured out a way to fix it in my app. The key for me was to make my app delegate class an extension of UIViewController rather than NSObject. (I think UIViewController is a subclass of NSObject anyway, so this shouldn't affect your app too much.)
In other words, change the main implementation line in your app delegate interface file from something like this:
#interface YourAppDelegate : NSObject <UIApplicationDelegate, UIActionSheetDelegate> {
To this:
#interface YourAppDelegate : UIViewController <UIApplicationDelegate, UIActionSheetDelegate> {
You should now be able to use the showInView: method with your action sheet within your app delegate implementation:
[yourActionSheet showInView:self.view];

Creating a new ViewController and xib for programmatic display (with addSubview)

I am writing a library to be used by developers for the iPhone (similar to the way that OpenFeint is implemented) and I am trying to create a ViewController with an associated XIB so that I can instantiate it in my code with
SplashScreenViewController *splashScreenViewController = [[SplashScreenViewController alloc] init];
UIWindow *topApplicationWindow = [self getTopWindow];
[topApplicationWindow addSubview:splashScreenViewController.view];
However, while this works with simple controls (UIButtons, etc), nothing shows up with my SplashScreenViewController. SplashScreenViewController is very simple:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface SplashScreenView : UIViewController {
}
#end
and the implementation is empty. In my View XIB (SplashScreenView.xib), I have tried setting the File's Owner's class to SplashScreenViewController which didn't work, then I tried it the way I've seen it done in OpenFeint which is to add a View Controller in IB and make the main UIView a child of it and make it of class SplashScreenViewController instead. That also does not work (does not display).
I'm wondering if anyone has a good idea for what I might be missing, or if someone can recommend a walkthrough for creating new ViewControllers the way that I'm attempting to.
Thanks!
Try 2 things :
Call initWithNibName not just init. Maybe the OpenFeint you were talking about were overriding the init to call initWithNibName , that's why you don't see it.
Set SplashScreenViewController as your file owner, and connect his view outlet to your
view in IB.
Hope it helps.
Instead of [splashScreenViewController alloc], try [SplashScreenViewController alloc]. I'm surprised you didn't get a compiler warning.