I have an App with a Tab Controller, which has obviously a certain number of tabs. Every ViewController in the TabController, share some informations, so I decided to move those in the AppDelegate and to synthesize everything in a NSDictionary so I can access to those using
[[[UIApplication sharedApplication] delegate] sharedInformations];
The solution works fine and I guess it's pretty good (I accept better solutions). Obviously, the compiler warns me about the fact that [[UIApplication sharedApplication] delegate] may not respond to the method sharedInformations because he didn't found the method in the protocols, but I know it will.
The question is: how do I suppress that warning?
You could cast it:
[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] sharedInformations];
or slightly tidier:
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
… = appDelegate.sharedInformation;
You could also encapsulate your shared info in a Singleton class and do something like
#import "MySharedInfo.h"
MySharedInfo *sharedInfo = [MySharedInfo sharedInfo];
// etc.
Related
I am trying to get the instance of ExtensionDelegate in InterfaceController in Apple Watch application but can't seem to find a way. I have created a method in Extension Delegate that I need to call and for that I would need a shared instance of Extension Delegate. Is there a way to do so?
Like for iOS App, we call it like:
[(AppDelegate *)[[UIApplication sharedApplication] delegate] methodName];
You can reference the ExtensionDelegate with the line below. Be sure to import WatchKit.
#import <WatchKit/WatchKit.h>
ExtensionDelegate* myDelegate = (ExtensionDelegate*)[[WKExtension sharedExtension] delegate];
[myDelegate methodName];
Use below code to get the same
ExtensionDelegate* myDelegate = (ExtensionDelegate*)[[WKExtension sharedExtension] delegate];
I have a textview on my viewcontroller.xib and I have a method to modify the text. I want to access that textview method on viewcontroller.m from a created object. I have a class called person that has in the init method to call the viewcontroller's method to update the text. Basically I want the textview to say "Person Created".
Here is the code I use inside my -init method of the person class which does not work:
ViewController * Viewscreen = (ViewController *) [[UIApplication sharedApplication] delegate];
[Viewscreen UpdateScreenText:#"Person Created!"];
ViewController * Viewscreen = (ViewController *) [[UIApplication sharedApplication] delegate];
This looks very strange to me, is your App Delegate really named ViewController? Are you trying to access a property declared in your App Delegate called Viewscreen?
If so then you would want to use the following to get a reference to it
ViewController * Viewscreen = [(ViewController *) [[UIApplication sharedApplication] delegate] Viewscreen];
Please clarify some more and we may be able to better assist.
I made a book for children. Every page is a ViewController. In the beginning, I did all the switching of the ViewControllers in the AppDelegate, but after having troubles with AutoRotation i did all the switching in another ViewController called ViewControllerSwitch.
Before, the code was like this. In the AppDelegate:
- (void)goToNextPage3 {
self.view3 = [[[ViewController3 alloc] init] autorelease];
view3.view.frame = CGRectMake(769, 0, 768, 1024);
[window addSubview:view3.view];
[UIView …SomeAnimationStuff...];
[UIView setAnimationDidStopSelector:#selector(animationDidStop3:finished:context:)];
}
- (void)animationDidStop3:(NSString *)animationID finished:(NSNumber *)finished context: (void *)context {
[self.view1a.view removeFromSuperview];
self.view1a = nil;
}
And here is the code from one of my view controllers ("pages") called ViewController1a:
- (void)buttonClicked {
MyBookAppDelegate* next2 =(MyBookAppDelegate *) [[UIApplication sharedApplication] delegate];
[next2 goToNextPage3];
}
This worked like a charm.
Now all my switching is in ViewControllerSwitch. How should I change the code in ViewController1a to access goToNextPage3?
I tried this:
- (void)buttonClicked {
ViewControllerSwitch* next2 = (ViewControllerSwitch *) [[UIApplication sharedApplication] delegate];
[next2 goToNextPage3];
}
It gives me a SIGABRT at [next2 goToNextPage3].
Any ideas?
Update:
i am still trying, so now i did this:
in my Viewcontroller1a.h:
#import <UIKit/UIKit.h>
#import "ViewControllerSwitch.h"
#class ViewController2;
#class ViewControllerSwitch;
#protocol ViewController1Delegate;
#interface ViewController1a : UIViewController
{
id<ViewController1aDelegate>myDelegate;
}
#property(nonatomic, assign)id<ViewController1Delegate>myDelegate;
#end
#protocol ViewController1Delegate
-(void)goToNextPageV;
#end
and in my .m file:
- (void)buttonClicked {
[self.myDelegate goToNextPageV];
}
i know there is something missing in the ViewControllerSwitch but i don´t know what.
As LordTwaroog said, this line is returning an object (your app delegate) of type MyBookAppDelegate:
[[UIApplication sharedApplication] delegate];
Then you're attempting to cast it to be an object of type ViewControllerSwitch with this:
(ViewControllerSwitch *)
This is incorrect. Your app delegate is an NSObject subclass conforming to the UIApplicationDelegate protocol. It's not a view controller.
The correct code might look like this:
- (void)buttonClicked {
ViewControllerSwitch* next2 = [ViewControllerSwitch alloc] initWithNibName:#"ViewControllerSwitch" bundle:nil]
[next2 goToNextPage3];
}
But depending on your app structure this might not be what you want. This is creating a brand new object of type ViewControllerSwitch. It's not returning the possibly-already-existing other ViewControllerSwitch object.
When you were using the app delegate to perform the switching, you had the benefit of being able to retrieve the existing app delegate object (rather than retrieving a newly created object of it's type). The app delegate is a singleton object, easily retrieved by calling [[UIApplication sharedApplication] delegate]. However, your ViewControllerSwitch object might not be set up as a singleton. So your access to it will depend on your object ownership structure. We'd have to see more of your code to help you with that.
[[UIApplication sharedApplication] delegate] still returns MyBookAppDelegate. Why do you try to get ViewControllerSwitch object out of it? You should use your ViewControllerSwitch object. Can you provide more code details on this object?
Possible solutions (if I understand you well):
Put your ViewControllerSwitch as an object in AppDelegate, so you could use:
ViewControllerSwitch *switch = [[[UIApplication sharedApplication] delegate] viewControllerSwitch]
Each ViewController can have a reference to your ViewControllerSwitch
Each ViewController should have a delegate with protocol (e.g.) ViewControllerDelegate, which will have a method to perform switching. Then after setting the delegate to appropriate object, you'll be able to switch your pages
MyAppDelegate *appD;
appD = [UIApplication sharedApplication];
if(appD.sw1.on)
NSLog(#"It is ON");
else
NSLog(#"It is OFF");
Gives no error while compiling. Runs without any warning, but it doesn't work.
I dont see what the problem is.
...
EDIT:
OMG, should have called delegate method too:
appD = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
I recommend you this method to share the app delegate:
https://coderwall.com/p/z4h4uw?i=2&p=1&q=&t%5B%5D=%21%21mine&t%5B%5D=%21%21bookmarks
and instantiated an appDelegate …
No, you haven't instantiated anything, you've just declared a variable that can point to your app delegate. But you haven't assigned anything to that variable yet.
And instantiating another object would be wrong here since the app delegate instance already exists. You just have to reference the existing app delegate and assign it to your variable:
appD = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
(Btw, you should follow the naming conventions. Class names should always begin with a capital letter.)
My AppDelegate_Phone class has the method -(void)doSomething. How can I call that from within a View Controller viewController?
I've tried:
[[[UIApplication sharedApplication] delegate] doSomething];
No luck.
doSomething not found in protocol
Edit. The action is being performed, but the Warning persists in XCode. What does the warning mean?
You have to cast the delegate to your custom delegate class then call the doSomething. Code example is below:
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[myAppDelegate doSomething];
The compiler is telling you exactly what is wrong (if a bit impenetrable because of the nested expressions); -delegate returns an object that responds to the UIApplicationDelegate protocol, of which your method is not a part of that protocol.
The most straightforward way to solve this is with a typecast:
[(AppDelegate_Phone *)[[UIApplication sharedApplication] delegate] doSomething];