Need a ref to the window in which a view is inside (no matter how deep inside). Usually there is only one window in an iPhone app. How would I access that the most easy way? Is there something cooler than getting the app delegate to access the window?
To get a currently used window by your application use:
[[UIApplication sharedApplication] keyWindow]
Well, UIView has the window property...
Related
I have an NSStatusItem that is properly displaying in the MenuBar. One of the items (when clicked) displays a modal NSWindow from my application, which is meant to perform a one-off task, then disappear. (Eg. the user enters a small bit of text, clicks "Save", and the modal NSWindow goes away.)
The issue occurs when the application is running in the background. The modal window properly appears above whatever application is running in the foreground, but when the user clicks the "Save" button, the rest of the application's windows also are made active. This is undesirable, as the user then has to click back to whatever app they were using. (Destroying the convenience of the NSStatusItem.) I'm displaying the modal window using:
[myWindow setFrame:finalRect display:YES animate:NO];
[myWindow setLevel:NSPopUpMenuWindowLevel];
[NSApp runModalForWindow:myWindow];
Is there any way to prevent clicks/events in my popup window from causing the rest of the application to become active? Or a way to let NSApp know that this particular panel shouldn't automatically activate the rest of the app? Thanks!
Instead of creating an NSWindow, create an NSPanel with the style NSNonactivatingPanelMask. You can then do the usual makeKeyAndOrderFront: and orderOut: to show/hide panel as needed.
NSApp's beginModalSessionForWindow, runModalSession, endModalSession are methods you need.
Have a look here for example how to use it:
Creating a fully customized NSAlert
A solution by Ken Thomases on the cocoa-dev list a couple years ago looks applicable here too:
[[NSApplication sharedApplication] hide:self];
[[NSApplication sharedApplication] performSelector:#selector(unhideWithoutActivation)
withObject:nil
afterDelay:0.05];
Which in theory tells the application to hide itself and unhide at the bottom of the window stack.
You could also intercept the mouse click event and use [NSApp preventWindowOrdering]
You can try something like:
...
if ([NSApp isHidden])
[myWindow makeKeyAndOrderFront:self];
else
[NSApp runModalForWindow:myWindow];
...
and when finish:
...
if ([NSApp isHidden])
[myWindow orderOut:self];
else
[NSApp stopModal];
...
I have a app that I am trying to reload the original viewcontroller from the MainViewController. There is no .xib the first view of the app is created in the AppDelegate. After I have loaded a couple UIViewControllers that did other things, I need to return to the original webView (ViewController) that was launched by the app on startup. I am really having a hard time getting anything to work that researched on the web. I just need a way to reload the original view that the app started with. Can anyone provide any possible solutions for me to try please? I am running out of options to try.
Thank!
You can set the rootViewController again.
In the app delegate use:
self.window.rootViewController = yourFirstViewController;
Good Luck!
EDIT:
Also, you can access to the app delegate globally using:
[[[UIApplication sharedApplication] delegate]
I published a little example project that will help you:
https://github.com/luisespinoza/ChangeViewController
Good Luck!
How do you get rid of the info bar displaying the battery, time, carrier, etc when running my app? Is it something I have to do using Objective-C, or is there an option within Interface Builder?
Go to your project info and add the key value:
Status bar initialy hidden. Set this on YES.
Or add this code to your app delegate:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
There is a way to turn it off in both Interface Builder and in code. It's usually referred to as the "status bar" in Apple's documentation.
This is probably some rookie mistake, but I can't figure it out. I've established a button within my app to recall a link in safari. From my method file it looks like this:
Obj C Code:
-(IBAction)linkSafari{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://en.wikipedia.org/"]];
}
I linked it up in IB and all seems okay.
When I create this same setup as a single view app it works great, but whenever I click it in my multiview app it's an automatic crash. Acts the same way on both my simulator and physical ipod.
Is there an endless list of places I could have screwed up or is there a certain area I should look into?
I don't think the problem is in that particular function,
but on the way you create the views and viewcontrollers
For anyone interested, this was solved thanks to an extra set of eyes. On my tab bar, I'd declared the assigned nibs for every section, but missed a few class associations. That's what messed me up.
I am writing an application where you need to show login screen modally and the app has a tab bar.
I have added tab bar directly to the UIWindow. To flip it to a new view (login view) I have overridden applicationDidFinishLaunching where I check if user has login credentials, then I do not show the login screen otherwise (assuming first time use or logout case) I modally present the login screen. I have given an option of logout in a settings tab inside the app.
I am using [[UIApplication sharedApplication] delegate] call to get instance of app delegate when user logs in first time. This way I get access to the tabBarController that is part of the Application Delegate (as is most of the times). However, when I try to call my loginViewController from the logout option in settings (somewhere in future life cycle), the same call [[UIApplication sharedApplication] delegate] returns me a delegate on which I am not able to use any of the methods I have defined. It gives me "unrecognized selector sent to instance" error at runtime.
I need to understand what exactly the call [[UIApplication sharedApplication] delegate] returns? Does the delegate object it returns change over the period of application life cycle? OR is it a singleton instance through out the app life cycle?
And secondly to resolve this, should I add the tabBar to a view (contained in main window) instead of adding it directly to the UIWindow (as done by the template for Tab Bar application and seems to be the standard practice). Are there any known problems with this approach OR its okay to do so. Any one has tried this? Please let me know.
Thanks
Dev.
It sounds like your class that gets an instance of your singleton delegate doesn't know what it implements. make sure you are #importing your delegate to the class that uses it as [[UIApplication sharedApplication] delegate]. Also, if you get a warning about UIApplication not conforming or whatever, you can cast it to your AppDelegate type to avoid it.
To answer your question about what this call returns, it is a singleton throughout the lifecycle of the app.
To answer the 2nd question, having it in the UIWindow (and thus in the appdelegate) is fine, and probably encouraged, since it is the root controller of your app (from the sound of things)