Open modal view and dismiss on iPhone - iphone

How do I display a full screen modal view and then if the user touches any where on the view, the view removes itself.

you can present a modal view that has a custom button as the background, and then when you press the button, or "background", you can call [self dismissModalViewControllerAnimated:YES];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
if ([touch tapCount] == 2) {
/* 2 touches here, you can dismiss your view */
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
if ([touch tapCount] == 1) {
/* 1 touch, dismiss your view */
}
}

If you were doing this inside of a subclass of UIViewController you could do something like this. This would trigger the modal to appear when the view is initially loaded, and trigger the modal to disappear when the screen is tapped.
-(void) viewDidLoad{
UIViewController *modalViewController = [[UIViewController alloc] init];
[modalViewController addTarget:self action:#selector(_dismissModal) forControlEvents:UIControlEventTouchUpInside];
[self presentModalViewController:modalViewController animated:YES];
}
-(void)_dismissModal{
[self dismissModalViewControllerAnimated:YES];
}

Put a custom UIButton on top of everything on the view you want to present modally. (A custom UIButton is transparent)
Hook up the UIButton's Touch Up Inside with one of the view's method (so that it can inform its delegate view controller about the event).
Implement a corresponding method in the delegate view controller so the modal view can be dismissed when the delegate hears from it.

Related

how to dismiss a UIPickerView in ios

In my app, i have pickerviews in my add contact page. I have given the following 'touchesBegan' method. But it dismisses the picker even when i click on a value to set. For example, if i click 'homephone' from preferred phone picker, the picker gets dismissed instead of setting the value. i need the picker to be dismissed only if i click outside the picker. Can anyone help me in fixing this.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.AddView endEditing:YES];
}
You can use
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] != pickerview)
[self.AddView endEditing:YES];
}
this will be used to endedit when the touch is outside the picker view
try this code:
UITapGestureRecognizer *single_tap_recognizer;
single_tap_recognizer = [[[UITapGestureRecognizer alloc]
initWithTarget : self.view
action : #selector(upper_button_view_tapped)]
autorelease];
[single_tap_recognizer setNumberOfTouchesRequired : 1];
[self.view addGestureRecognizer : single_tap_recognizer];
-(void)upper_button_view_tapped
{
[self .view endEditing:YES];
}
Hope it help You
In your code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self dismissViewControllerAnimated:YES completion:nil];
}
Or You can put a bigger button under the PickerView, make it color clear custom and dimension full screen, then use the dismiss inside a (IBAction) and when you tap outside the picker your view is dismissed
- (IBAction)buttonClear:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

UITabBarController, any possible way to move next View Controller with horizontal swipe?

I am new to iOS. Not sure if there is a possiblity to bring the following logic.
1. UITabBarController has three UIViewController.
2. While Swipping horizontally in the second UIViewController, i want to move
to the third UIViewController.
Is it possible?. if so, kindly advice how to achieve the same.
In the Second ViewController add the following lines in ViewDidLoad
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(gotoNextPage:)];
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
And add method
-(void)gotoNextPage:(id)sender {
[self.tabBarController setSelectedViewController:thirdViewController];
}
You can use Touch Delegate Method i am doing to pop to previous view
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint startPosoition = [touch previousLocationInView:self.view];
CGPoint endPosition = [touch locationInView:self.view];
if (endPosition.x - startPosoition.x > 20)
{
[self.navigationController popViewControllerAnimated:YES];
} else
{
}
}
Yes that can be done. For 1. I assume you know how to setup a tabbar based app with viewcontrollers. For 2. Implement the swipe gesture recognizer on your second view controller's view and in the gesture handler do: [self.yourTabBarController setSelectedIndex:2];
Use this code;
UISwipeGestureRecognizer *swipeGestureNextPage = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(loadPreviousPageClicked)];
swipeGestureNextPage.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeGestureNextPage];
-(void)loadPreviousPageClicked
{
//use navigation controller here accordingly
}
Thats it.

Subview outside of main view does not receive touch events

In my application i have a main screen, which has a subview (this is another screen), but this subview is moved 320 to the right so it is just offscreen. The effect is that the main screen moves 320 to the left, which hides the main screen and shows the other screen because it's a subview. The problem is, the other screen is not receiving touch events because it is out of the bounds of the main screen (superview). How do i get this other screen to also receive touch events so i can tap the buttons?
Have a look at this post on S.O. for an explanation. The solution is to implement your own hitTest using the following code:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
BOOL isInside = [super pointInside:point withEvent:event];
// identify the button view subclass
UIButton *b = (UIButton *)[self viewWithTag:3232];
CGPoint inButtonSpace = [self convertPoint:point toView:b];
BOOL isInsideButton = [b pointInside:inButtonSpace withEvent:nil];
if (YES == isInsideButton) {
return isInsideButton;
} // if (YES == isInsideButton)
return isInside;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *v = nil;
v = [super hitTest:point withEvent:event];
return v;
}

Why isn't my UIViewController responding to touches?

I have a UIViewController that contains a UIScrollView, which has a UIView inside of its contentview.
I have the following code that does not work, keyboard is not dismissed, why?:
#pragma mark -
#pragma mark Touch Events
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == scrollView || [touch view] == self.view)
{
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
}
touchesBegan:withEvent: is a UIView method, not a UIViewController method. What are you trying to achieve here? You should very seldom have a UI reaction to touchesBegan:. You probably mean to use UITapGestureRecognizer instead.
Make sure to use accessors (self.scrollView) rather than accessing your ivars directly. Direct ivar access is the #1 cause of memory management crashes.

Hiding the keyboard when UITextField loses focus

I've seen some threads about how to dismiss the Keyboard when a UITextField loses focus, but it did not work for me and I don't know how. The "touchesBegan:withEvent:" in the following code, never gets called. Why?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([self.textFieldOnFocus isFirstResponder] && [touch view] != self.textFieldOnFocus) {
[textFieldOnFocus resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
P.S.: This code has been inserted in the view controller which has a UITableView. The UITextField is in a cell from this table.
So, my opinion is: this method is not being called, cause the touch occurs on the UITableView from my ViewController. So, I think, that to I should have to subclass the UITableView, to use this method as I have seen on other Threads, but it may have a easier way.
Could you please help me? Thanks a lot!
Make sure you set the delegate of the UITextField to First Responder in IB. And I just put a custom (invisible) UIButton over the screen and set up an IBAction to hide the keyboard. Ex:
- (IBAction)hideKeyboard {
[someTextField resignFirstResponder];
}
With that hooked up to a UIButton.
Here is my solution, somewhat inspired by several posts in SO: Simply handle the tap gesture in the context of the View, the user is 'obviously' trying to leave the focus of the UITextField.
-(void)handleViewTapGesture:(UITapGestureRecognizer *)gesture
{
[self endEditing:YES];
}
This is implemented in the ViewController. The handler is added as a gesture recognizer to the appropriate View in the View property's setter:
-(void) setLoginView:(LoginView *)loginView
{
_loginView = loginView;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.loginView action:#selector(handleTapGesture:)];
[tapRecognizer setDelegate:self]; // self implements the UIGestureRecognizerDelegate protocol
[self.loginView addGestureRecognizer:tapRecognizer];
}
The handler could be defined in the View as well. If you are unfamiliar with handling gestures, see Apple's docs are tons of samples elsewhere.
I should mention that you will need some additional code to make sure other controls get taps, you need a delegate that implements the UIGestureRecognizerDelegate protocol and this method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]]) // Customize appropriately.
return NO; // Don't let the custom gestureRecognizer handle the touch
return YES;
}
-(void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
for (UIView* view in self.view.subviews)
{
if ([view isKindOfClass:[UITextField class]])
[view resignFirstResponder];
}
}