Open window in macOS application in modal mode [duplicate] - swift

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

Related

iOS 5 UIWindow makeKeyAndVisible closes modal

I have a scenario where I present a modal view controller from the main window's rootViewController. While it is loading the contents of this modal, I am switching to another loading window which I make key and visible.
This window acts as a loading indicator and does not allow the user to interact with the app. When it is done loading, I switch back to the main window by making it key and visible. When I do this, the modal is force closed and the app is no longer able to present modals.
Interestingly enough, if I execute [UIWindow makeKeyWindow] when switching back to the main window, there are no issues. [UIWindow makeKeyAndVisible] is what's causing the issue. Is [UIWindow makeKeyWindow] an acceptable alternative?
This is iOS 5 only. No issues in iOS 6. I am not supporting iOS 4. Does anyone know what might be happening here?
I got the same behaviour, and as documentation states that makeKeyAndVisible method is a convenience method it seems to me legit to substitute makeKeyAndVisible call with working code:
[window makeKeyWindow];
window.hidden = NO;
Have no idea what's wrong, but it looks like a bug.
It is definetly bug in iOS 5 UIWindow implemetation. I faced this strange behaviour and Aleksey's answer is the way to go.

Add Splash-screen for when App Launches (Even From Multitasking)?

I need to add a short splash-screen to my app, which appears even when the app is launched from multitasking.
I have managed to do so, popping an UIImageView up and fading it out after a couple of seconds form the - (void)applicationWillEnterForeground:(UIApplication *)application method in my app delegate.
However when launching from multitasking, while it appears and works fairly well, sometimes there a short delay before the image pops up and so you briefly see the view behind it.
How can I overcome this?
Thanks.
ok, now I understand -
then you may try to use
- (void)applicationDidBecomeActive:(UIApplication *)application
If I understand correctly you're just trying to create a launch image right? So in that case you would simply go into your info-plist in Supporting Files then highlight one of the items and press the "+" icon. Select "Launch Image" from the drag down menu, and type the name of your image into the text box next to "Launch Image".
Try this.
Create a reference (a strong reference if using ARC) to the view, so you hold it. Then create the view anywhere in the program really (I guess at first start would be good). Then, when you return from multitasking, you don't need to create a new view (which might take time), you only push the view that you already have set up in-memory.
Assuming you don't do this already, it should significantly shorten (or remove) the delay you're seeing.
Also, don't forget to set animated: to NO

can any one tell me the name of alert view that should do the works of **C# show dialog**

can any one tell me the name of alert view that should do the works of C# show dialog .
other actions will not work without dismissing it(EVEN DELEGATES ALSO).
Modal dialogs in Windows (in any language) and in iOS are fundamentally different.
On Windows, a modal dialog (and in particular the ShowDialog method) behaves like a function call that will only return when the dialog is closed. Your app will naturally wait until the your has made his decision.
On iOS, presentModalViewController (or [UIAlertView show]) almost immediately return. You can registered a delegate that will be notified when the dialog is closed. But if your app is supposed to wait only the user has chosen something in the dialog, then you have to implement the waiting yourself.
As far as know, You have to do this manually. You can lock your task when the alert is shown and then unlock it again when it is dismissed getting help from these posts.
is there a way to pause an NSTHread indefinitely and have it resumed from another thread?
How to pause an NSThread until notified?

Why UIView keep same as last close without saving?

I'm developing a iphone app based UITabBarController,when open the app,on the first tab item,
I press a button,to add a label to the view,and I close the app.
Next open,the label which added last time still on the first tab item,I did not save the view
in applicationWillTerminate method,why?
And,if I switch to the second tab item,and return the first tab item,the view of first stay as before,is the UIView instance keep alive during the app running time,or what reason?
Thanks.
This is the default behaviour of the tab bar controller. If you want to change it, you have to override the UITabBarControllerDelegate.
And also from iPhone OS 4.0 when you close the application pressing the home button, the app is not really closed. It goes background.
If you want your tab bar controller to work as you mention, go to the initial view. You have to override some App Delegate methods, like
- (void)applicationWillResignActive:(UIApplication *)application
or
- (void)applicationDidEnterBackground:(UIApplication *)application

IPhone UITextField not showing edit caret or clearing placeholder text when tapped

I have a window within an iPhone application, which is displayed modally to allow the user to enter their settings for a web service upon 'first run'.
The text fields have helper text set, and when you tap them the keyboard shows and allows you to enter text.
Unfortunately the text fields do not clear the helper text, show the edit caret or show the text being entered (as in the screenshot below).
Any suggestions?
The window is being displayed with [self presentModalViewController:<controller_name> animated:YES];, which may or may not be the cause of this issue - when I run the UI via the Interface Builder 'test' application the text boxes respond like normal.
Clear when editing begins has been set for both fields.
Thanks in advance!
Edited: More information
After the info Bart Gottschalk provided I thought I should add some more information. First, the application is a Navigation Based Application.
Secondly, the test app Bart recommended worked fine, so that takes the modal window and the view out of the equation.
Third, I was presenting the modal view when the -(void)viewWillAppear... delegate method was being called - which may very well be the wrong place... however I'm not 100% sure if I should be presenting the modal view from within the didFinishLaunchingWithOptions of the App Delegate...
(this is happening on Simulator and iPhone 3.1.3)
In Interface Builder did you check the box for "Clear When Editing Begins"? With that checked the text field should clear any value once the use taps to edit which is the behavior I think you're looking for.
You can also set the same property programatically using clearsOnBeginEditing if that is convenient in your code.
My guess is that you've done this and it's not behaving as you expect. Just checking on this as a first step in helping you debug.
Also, does this happen in both the Simulator and on a testing device?
Bart
Edited Below...
This seems strange. Let's strip away everything but the basics of presenting a modal view when the application starts and see what happens.
I've recreated the most basic app (that I know of) to test presenting a modal view controller at launch and verify that field editing works fine. What happens for you when you do the same/similar in a new project?
Here is what I'm doing:
1) Create a new view-based app in Xcode called "ModalViewTest"
2) Create a new UIViewController with xib called ModalViewController
3) In ModalViewController.h add a method
-(IBAction)closeModalView;
4) In ModalViewController.m add the method implementation as
-(IBAction)closeModalView {
[self dismissModalViewControllerAnimated:YES];
}
5) In the ModalViewController.xib create two text fields and set the placeholder text for each to abcd1234 and confirm that "Clear When Editing Begins" is checked.
6) In the ModalViewController.xib add a button "Close" and set Touch Up Inside to fire "closeModalView"
7) In the application delegate (ModalViewTestAppDelegate) add the following import
#import "ModalViewController.h"
8) In the application delegate (ModalViewTestAppDelegate) applicationDidFinishLaunching add the following after the line containing [window makeKeyAndVisible];
ModalViewController *modalViewController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[viewController presentModalViewController:modalViewController animated:YES];
9) Save everything
10) Build and Run this new app
Does editing of the text fields work as expected? If yes, what is different about how you are building and presenting your modalView? If no, then we'll need to dig further to determine what is going on in your environment.
Second Edit Below...
When creating a navigation-based application I did the following to present the modal view at application start. Does this work for you in both your test app as well as your real app?
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
ModalViewController *modalViewController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[navigationController presentModalViewController:modalViewController animated:YES];
}
Well, I just figured it out, but honestly without the persistence and awesome help from Bart it would have taken much longer and been much more frustrating.
It turns out the problem was that I was using a Window instead of a View in the XIB file. This was why when showing the modal view within the Navigation controller it wouldn't display properly (i.e. only a white screen) and why the UITextField would not work properly when showing the view from the RootViewController.
So, to recap - modal views should have UIView, not UIWindow in the XIB/NIB File.
Thanks for your help Bart!
I have the same problem but in iOS7 only. I solved it by changing the tint color of textField to blue in the Storyboard