MonoTouch ResignFirstResponder without knowing which UIView is it - c#

I'm building a MonoTouch iPhone app, but have come up with a small issue.
I have a view with 2 UITextFields for login (username/password), however when I first tap on a field, it pops up the keyboard, and the keyboard never disappears again.
The problem is the keyboard hides my "Login" button, so the user cannot tap it.
I'd like the keyboard to resign if I tap anywhere on the "background" of my view.
So I figured out I can do that in TouchesEnded on my UIViewController - however to resign the keyboard I need a reference to the current UIView being FirstResponder.
Which finally leads to my question: Can I find out which UIView is currently FirstResponder without looping recursively through all views from my UIViewController ?

You can use
this.View.EndEditing(true);

Related

Keyboard not showing when being prompted by UITextField

In iOS 13.2, I'm noticing that my keyboard no longer shows up in my apps on the simulator and a real device. When I tap inside a UITextField, nothing happens but the cursor blinking inside the textfield. Is anyone else having this problem or know how to solve?
UPDATE
The problem originates from removing storyboard files and initiating a rootViewController programmatically
var windowScene = new UIWindowScene(session, connectionOptions);
Window = new UIWindow(windowScene);
Window.RootViewController = new ViewController();
Window.MakeKeyAndVisible();
After that, I just noticed the keyboard doesn't show again in a new project.
This looks like a bug when you try use a programatic approach to setting a rootViewController in the new SceneDelegate opposed to how we used to in AppDelegate.
I got around this by keeping the dumb storyboard which sets the rootViewController inside and keeping SceneDelegate.cs clean for the moment and my keyboard works again. Thanks for all your answers.
set textfiled delegate.
#IBOutlet weak var urlTextField: UITextField!
override func viewDidLoad() {
self.urlTextField.delegate = self // urlTextField it is your textfield outlet
}
If you delete this line of code, the keyboard does not appear, but if this exists, the keyboard does come up.
About simulator : You can check setting in iOS simulator firsrt .
Have a look at this screenshot ,be sure Use Hardware Keyboard not be selected . Or you can select it and unselect it again , then keyboard will show.
Restart app to check whether keyboard can be shown.

How can I open page in my Xamarin app without using navigation?

I have a Xamarin Forms application, which has 2 buttons on the toolbar. Currently I'm using FreshMVVM (and it's navigation). I want to load pages without putting them into the navigation. I do not want to have the 'back' button on the toolbar, also I do not want the user return the last page.
Here's how I push a page currently:
CoreMethods.PushPageModel<FirstChoicePageModel>();
I tried to push as a modal, but that way the toolbar buttons does not work until I press back. Should I make new navigation containers and switch to them if I push the buttons?
It's been a little while since I've used FreshMVVM, but if I recall correctly, you have 2 options:
If you want to completly reset the nav stack, you can do CoreMethods.PushPageModelWithNewNavigation<FirstChoicePageModel>();
If you want to keep the nav stack, you can set NavigationPage.SetHasBackButton(this, false); in the code behind of the view.
For both of these options, it may necessary to intercept the back button on Android. In the code behind there is an overrideable method:
protected override bool OnBackButtonPressed()
{
return true; // prevent Xamarin.Forms from processing back button
}
If you want to show a page, but you doesn't want to navigate (change the current page), you can always use Popups.
See this link https://github.com/rotorgames/Rg.Plugins.Popup

How do I force selection of a keyboard type in a Xamarin.Webkit.Webview

We have a Xamarin app (Android) that at one stage opens up a web view (Webkit.Webview not Forms.Webview). This directs the user to a page on a third party site which has been set up for us.
Firstly - on certain input fields the keyboard which shows up is the wrong one - we are expecting a dismissable keyboard (i.e. "Done" in the bottom corner, not a "Submit"). I know this can be changed but not sure what is the correct way to do this. Does it have to be the metadata/text inputs on the web page that is changed? If so - what needs to be modified per text box entry on the html of the page? Just the type? i.e:
<input type="email">
Secondly, rather than wait for the third party to fix the page, is there a way we can force the webview to always open a certain keyboard type?
We have an option of intercepting the keyboard key presses and trying to dismiss the keyboard on return press at the minute. But would prefer not to put a hack in that intercepts every key press.
Appreciate the help, not sure what the way forward is here.
Thanks
From the comments: To your second question about forcing a keyboard button, you can check out this link which describes how to override OnCreateInputConnection to specify the Keyboard Enter Button type.
public class MyWebView : WebView {
...
public override IInputConnection OnCreateInputConnection (EditorInfo outAttrs) {
var inputConnection = base.OnCreateInputConnection (outAttrs);
// outAttrs.ImeOptions in Xamarin only allows ImeFlags but it also should allow ImeActions
outAttrs.ImeOptions = outAttrs.ImeOptions | (ImeFlags)ImeAction.Next;
return inputConnection;
}
}
That will not dismiss your keyboard when tapped though since it is meant to take the user to the next input. Hopefully someone else can come along and either provide a better answer or give a good way to dismiss the keyboard in this situation without hacking something together.

Want to create a UIContoller in a UITabController but with no tab button

I want to display an intro view (tutorial) on my monotouch app that when the user clicks on a button will take them to the main storyboard, which contains a UITabController.
I'm new to monotouch and can't work out how to do this. I'm adding an extra view that's been created to the tab bar controller in the AppDelegate.FinishedLaunching, but this always adds the button to the tab bar.
When the user has clicked the button once I don't want to show the intro page ever again, it's a one time deal (I'll save some value to disk to work this out), so I don't want to just add it to the tab controller.
Incidentally if anyone can show me where monotouch decides that it's going to start with the storyboard please let me know. The only thing I've found is the little start arrow that you drag around in XCode, but what if I have two storyboards and I want to load one based on the user being logged in or something.
You need to create simple entry ViewController with the button (Controller1). The next controller in storyboard will be the tabcontroller (TabController).
Thus you will always has the first entry screen in your application.
If you don't want to show it later than make transition from Controller1 to TabController before it is loaded. For example, override the ViewWillAppear method.
The second approach. Use this code to launch whatever you want view above all your controllers at any time:
UIViewController root= UIApplication.SharedApplication.KeyWindow.RootViewController
UIView myCustomView=new MyCustomView();
root.Add(myCustomView);
//call myCustomView.RemoveFromSuperview() and it will be dismissed

WPF and Prism View Overlay

I need some help with overlaying views using the prism framework.Its a little more complexed than that so let me explain.I could be over-thinking this as well :D
i have shell (wpf window) and i have 2 views(A & B - both usercontrols) in a module.
when the shell loads it loads view A. On view A i have a button to "popup" view B
for some user input. so naturally i would think to some sort of modal window/control, maybe even a popup. however the problem i face with the popup is that when i move the shell the popup remains fixed and it doesnt block events in view A. I've tried disabling view A to stop events being fired and i've also tried to use a to get the view B move with the shell. Only the canvas works but i now need a way to block it tho'. Is there anyway i can overlay a view on top of another view with prism? or how does everyone else create modal popups with prism & wpf? any advise or pointers would be greatly appreciated.
If you want to use embedded dialogs without an extra window, you can use Prism's RegionManager to achieve the outlined behavior. The trick is to put the PopUp region parallel to your main region in the visual tree:
<Grid>
<ContentControl cal:RegionManager.RegionName="MainRegion" IsEnabled={Binding IsNoPopUpActive} />
<ContentControl cal:RegionManager.RegionName="PopUpRegion"/>
</Grid>
Now use the RegionManager to put view "A" into the "MainRegion". Create a controller class similar to IPopUpDialogController. It should be responsible for putting your view "B" (or any other PopUpView in your application) into the "PopUpRegion" on demand. Addtionally, it should control a flag that signal the underlying "MainRegion" to be enabled or disabled. This way a user won't be able to play with the controls in your view "A" until the pop up is closed.
This can even be done in a modal fashion by using ComponentDispatcher.PushModal() before pushing a frame onto the Dispatcher. However, I would recommend avoid modal dialogs.
Update: As requested in a comment, the IsNoPopUpActive could be implemented in the backing view model. There you could link it to RegionManager's View collection for the popup region:
public bool IsNoPopUpActive
{
get { return _regionManager.Regions["PopUpRegion"].Views.Count() == 0; }
}
Remember to trigger a PropertyChanged event as soon as you modify the views collection (add/remove a popup).
Just for your information: nowadays I avoid disabling the controls in the background and instead insert a transparent panel. This avoids clicking on background controls. However, this does not handle keyboard input (tab-ing to controls). To fix the keyboard input you need to make sure that the keyboard focus is trapped in the popup (MSDN on WPF Focus concepts).
Adding the following focus attributes to the popup region should do the trick:
KeyboardNavigation.DirectionalNavigation="None"
KeyboardNavigation.ControlTabNavigation="None"
KeyboardNavigation.TabNavigation="Cycle"
KeyboardNavigation.TabIndex="-1"
If you are using WPF + MVVM with Prism you can take a look at this Message View overlay controller. The nice part about this approach is you can write unit tests on you view model using a mock overlay controller and have the mock controller return the result that the user would choose in the overlay.
You can find it here: http://presentationlayer.wordpress.com/2011/05/24/wpf-overlay-message-view-controller/
Hope this helps

Categories