why we need to implement UiNavigationcontroller delegate in ImagePickerController in iphone - iphone

hello all I am using Uiimagepickercontroller to record a video there I need to implement two delegates.. 1 is UINavigationcontroller delegate and 2 is UIImagepickercontroller delegate.If we not use navigationcontroller delegate its giving me warning at the code picker.delegte=self;
what is the necessity of UINavigationControllerDelegate..
My requirement is I am recording a vedio automatically.. so after it stops recording It should go to the Screen saying that useThis ,Discard options and it plays the video what is captured?
Is there any way.. to do that ..
could any one tell me the way for this and what is the UINavigtaioncontrollerdelegate exactly?
Thank you all

UIImagePickerController is a subclass of UINavigationController. It requires its delegate to implement the UIImagePickerControllerDelegate protocol while still implementing the UINavigationControllerDelegate protocol for its superclass.
That means you need to declare your delegate class like this:
#interface MyDelegate : NSObject <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
And then implement the required methods for both protocols.

Related

How can I simply store URL data between UIViews?

I need to transfer NSURL data to target UIViewController for UIWebView. How can I do it to load required url from ViewDidLoad of target view? I am using storyboard, ARC, iOS5.
You could use a delegate to send the URL from UIViewController to UIWebView.
In your webview create a delegate property:
#property(nonatomic,weak) (id) delegate;
Synthezize as normal. Then in the webviews viewDidLoad method write:
self.someWebViewURLProperty = self.delegate.someURLPropertyFromUIController;
Now set the delegate in your ViewController. In your UIViewController in the viewDidLoad method write:
self.webViewProperty.delegate = self;
It may seem a bit more involved than using prepareForSegue, but both methods are valid. I prefer delegation as its a standard cocoa design pattern.

Assigning ViewController to a delegate, Is it good?

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>

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];

Setting delegate in subclass when theres two possible delegates in parent classes

I'm getting a warning when I set the delegate for UIImagePickerController. It's because UIImagePickerController and its parent UINavigationController both have delegates that can be used. The code works fine but just wondering how to handle delegates and inheritance properly and lose the warning.
So basically I've created my own MyImagePickerController which is subclass UIImagePickerController.
UIImagePickerController is a sub class of UINavigationController.
so the inheritance tree is
UINavigationController > UIImagePickerController > MyImagePickerController
MyImagePickerController is also the delegate for UIImagePickerController.
So I add to my #interface.
#interface MyImagePickerController : UIImagePickerController <UIImagePickerControllerDelegate>...
Then I set MyImagePickerController to be the delegate to itself in loadView
- (void) loadView
{
...
self.delegate = self;
...
}
And I implement the UIImagePickerControllerDelegate delegate methods and it all works.
But I get a warning
warning: class 'MyImagePickerController' does not implement the 'UINavigationControllerDelegate' protocol
The problem is that both parent classes have their own Delegates
UINavigationController > UIImagePickerController > MyImagePickerController
UINavigationControllerDelegate > UIImagePickerControllerDelegate > MyImagePickerController
And in the definition for UIImagePickerController the delegate method can take either UINavigationControllerDelegate or UIImagePickerControllerDelegate
#interface UIImagePickerController : UINavigationController <NSCoding> {
#property(nonatomic,assign) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
The app works, the UIImagePickerControllerDelegate methods in MyImagePickerController are being called but is there a proper way to set the delegate so someone reading the code knows we're trying to implement UIImagePickerControllerDelegate methods and for a newbie to clear the warning.
And what happens if you want to implement both UIImagePickerControllerDelegate and UINavigationControllerDelegate methods in the UIImagePickerControllerDelegate? How would you set the single delegate in Obj-C?
Cheers
Read the warning. It tells you everything you need to know.
warning: class
'MyImagePickerController' does not
implement the
'UINavigationControllerDelegate'
protocol
This Line:
#property(nonatomic,assign) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
Does not mean that the delegate object (Note delegate object, not delegate method) can be a UINavigationControllerDelegate or a UIImagePickerControllerDelegate. It means that the delegate object must be a UINavigationControllerDelegate and a UIImagePickerControllerDelegate (it must conform to both these protocols).
You seem to be slightly confused about subclassing and inheritance. The problem isn't that both parent classes have their own Delegates. UINavigationController has a delegate property UINavigationControllerDelegate and because UIImagePickerController is a UINavigationController it also has this delegate, which is still UINavigationControllerDelegate, but it has also added extra behaviour, as subclasses tend to do, and the delegate must now not only implement UINavigationControllerDelegate methods but also UIImagePickerControllerDelegate methods.
So the problem isn't "what happens if you want to implement both..". You have to implement both.