UITextField keyboard dismiss on button click to UIApplication method - objective-c

I'm going from storyboard to UISplitViewController in a xib. I want to logout of my storyboard view, which will bring me to the UISplitViewController. In the storyboard controller, I have a UITextField. I set it so that it doesn't dismiss itself after I press enter. However, it now stays open even when I switch to the splitviewcontroller, which I obviously don't want because i want to logout.
Logout Button
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithTitle:#"Log Out" style:UIBarButtonItemStylePlain target:self action:#selector(confirmLogout)];
Method to bring up UISplitViewCOntroller
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==TAG_DEV){
if (buttonIndex) {
[[[UIApplication sharedApplication] delegate] performSelector:#selector(gotoSplitViewController)];
} else {
[self.navigationController popViewControllerAnimated:YES];
}}
}
method gotoSplitViewController in the delegate:
-(void)gotoSplitViewController{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *listNC = [[UINavigationController alloc] initWithRootViewController:listVC];
AssignedViewController *assignedVC = [[AssignedViewController alloc] initWithNibName:#"AssignedViewController" bundle:nil];
UINavigationController *assignedNC = [[UINavigationController alloc] initWithRootViewController:assignedVC];
//assign the views to the new nav view controllers
splitViewController = [[UISplitViewController alloc] init];
splitViewController.delegate = assignedVC;
splitViewController.viewControllers = #[listNC, assignedNC];
listVC.detailViewController = assignedVC;
[[self window] setRootViewController:splitViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
}
my UITextField
self.textBox = [[UITextField alloc] initWithFrame: CGRectMake(10, 660, 750, 90)];
self.textBox.backgroundColor = [UIColor colorWithRed:(209/255.0) green:(236/255.0) blue:(232/255.0) alpha:1];
self.textBox.returnKeyType = UIReturnKeySend;
self.textBox.delegate = self;
[self.view addSubview:self.textBox];
I've tried the resignFirstResponder if various places, including adding
- (void)viewWillDisappear:(BOOL)animated
{
[self.textBox resignFirstResponder];
}
However, none of them work. How do I get the keyboard to disappear after the views change?

I set
self.chatBox.delegate=nil;
in my methods to switch views

Related

Trying to add BarButtonItem to programmatically created NavigationController

I have pushed a view controller embedded in a navigation controller after tapping a button in my app. I am trying to add a "SAVE" button in the right corner of the nav bar. However, with this code it is not showing up. My assumption is that it is not showing up because nc.navigationItem.rightBarButtonItem should be placed in viewWillAppear. Since nc is programmatically created and does not have a class file with viewWillAppear how can I do this?
Here is my code:
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
FXFormViewController *vc = [[FXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
// WHY IS THIS BUTTON NOT BEING ADDED?
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
[self presentViewController:nc animated:YES completion:nil];
}
The UINavigationController uses the -navigationItem of the currently displayed view controller. Try this instead:
vc.navigationItem.rightBarButtonItem = ...
Here you can take advantage of a powerful feature of OOP called Inheritance.
Create a Class say MyFXFormViewController inherited from FXFormViewController.
MyFXFormViewController.h
#import "FXFormViewController.h"
#interface MyFXFormViewController : FXFormViewController
#end
MyFXFormViewController.m
#import "MyFXFormViewController.h"
#implementation MyFXFormViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = saveBarButton;
}
And your previous code should be like
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
MyFXFormViewController *vc = [[MyFXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
[self presentViewController:nc animated:YES completion:nil];
}

Shift to UIPopoverPresentationController

I want to create the same popover for my iPhone app with size GGrect of (320 100.0).
Here is my old code:
View * pk1 = [[View alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:pk1];
pop = [[UIPopoverController alloc] initWithContentViewController:nav];
[pop setDelegate:self];
[pop presentPopoverFromRect:[self btnsavepart].frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[nav release];
I using this codes but its doesn't change
nav.preferredContentSize = CGSizeMake(320, 100);
I can not change UIpopovercontriller with UIPopoverPresentationController.
How do I specify GGrect of (320 100.0)?
i thinks i can change only on horizontally but how can i do.
View *viewController = [[[View alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
navController.modalPresentationStyle = UIModalPresentationPopover;
navController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
navController.popoverPresentationController.sourceRect = btnsavepart.bounds;
navController.popoverPresentationController.sourceView = btnsavepart;
navController.popoverPresentationController.delegate = self;
[self presentViewController:navController animated:YES completion:nil];
i want the UIPopoverPresentationController not FullScreen form in iPhone
i adding this codes but i give the FullScreen view
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
return NO;
}
I work with Xamarin but its same thing , to specify size of the popover in which you want to display your UIViewController, in the properties of your UIVIewController you have the PreferredContentSize property. There you specify the size.
I saw it somewhere here but forgot where.

Touch on ImageView to another ViewController

I have 2 ViewControllers and I create one UIImageView to show like Splash Screen on IPhone. I write it in TestAppDelegate.m :
====================
splashScreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashScreen.image = [UIImage imageNamed:#"Default.png"];
[self.window addSubview:splashScreen];
sleep(6);
[splashScreen removeFromSuperview];
====================
My question is,
if I touch on this imageview I will go to 2nd ViewController.
else after time sleep, automatically to 1st ViewController.
So, it's possible to do that ?
Do this:
Add UIGestureRecognizerDelegate in appDelegate.h file.
#interface AppDelegate : UIResponder <UIApplicationDelegate,UIGestureRecognizerDelegate>
Now
splashScreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashScreen.userInteractionEnabled = YES;
splashScreen.image = [UIImage imageNamed:#"Default.png"];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.delegate = self;
[splashScreen addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
[self.window addSubview:splashScreen];
sleep(6);
[splashScreen removeFromSuperview];
//add ViewController1 here
ViewController1 *objViewController1 = [[ViewController1 alloc]initWithNibName:#"ViewController1" bundle:nil];
[self.window addSubview:objViewController1.view];
Now handler will be called when tapped on splash screen
- (void)handleTap:(UITapGestureRecognizer*)recognizer
{
// Do Your thing.
if (recognizer.state == UIGestureRecognizerStateEnded)
{
[splashScreen removeFromSuperview]; //edited here
ViewController2 *objViewController2 = [[ViewController2 alloc]initWithNibName:#"ViewController2" bundle:nil];
[self.window addSubview:objViewController2.view];
}
}
Yes. It's possible.In AppDelegate keep NSTimer.
In the selector of timer write code to push to the 1st view controller.
And put a Touch Recognizer on the imageview and on the touch event write code to push to the 2nd View Controller.

UIButton UIPopUpController programmatically

having difficulty getting UIButton Items to show.. I want to place several UIButtons within my UIPopUpController. I'm doing everything in a storyboard and I need to do this section programmatically.
- (void)showPopover:(id)sender
{
if(![popoverController isPopoverVisible])
{
myPopOver = [[PopUp alloc] init];
popoverController = [[UIPopoverController alloc] initWithContentViewController:myPopOver] ;
[popoverController setPopoverContentSize:CGSizeMake(300, 200.0f)];
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
CGRect rect = (CGRect)[sender frame];
rect.origin.y = self.view.frame.size.height - rect.size.height;
[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
[popoverController dismissPopoverAnimated:YES];
}
}
You could use a UINavigationController (it has already a navigation bar where you can attach buttons items) and add it as the content of your UIPopoverController. The UINavigationController can be istantiated using the initWithRootViewController method. In this case the controller would be your Popup class. For example, inside your showPopover method, you could do the following:
PopUp* myPopOver = [[PopUp alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:myPopOver];
popoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
// do other stuff here to present you UIPopoverController
[myPopOver release];
[navController release];
Now, inside your PopUp class, within viewDidLoad method, you can customize the UINavigationController navigationBar. For example:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *aButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(somethingSelector:)];
self.navigationItem.rightBarButtonItem = aButtonItem;
[aButtonItem release];
}
where somethingSelector is like the following:
- (void)somethingSelector:(id)sender
{
// your custom actions
}
EDIT
Alternatively you can avoid to use the UINavigationController and create UIToolbar inside your PopUp class. Inside the UIToolbar you can attach UIBarButtonItems. See UIToolbar Class Reference

Adding UIBarButtonItem to UINav..Controller

i am not sure what i am missing here. I Have a custom UINavigationController and i am trying to add a persistant UIBarButtonItem to the bar.
-(void)viewDidLoad
{
self.navigationBar.barStyle = UIBarStyleBlack;
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:#"Nope..."
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goBack:)];
self.navigationItem.leftBarButtonItem =bbi;
[bbi release];
}
-(void)goBack:(id)sender
{
NSLog(#"go back now");
}
what am i missing here? - BTW, i do not want to/ will not use IB.
UPDATE:
Currently this is the closest i can get:
-(void)viewDidLoad
{
self.navigationBar.barStyle = UIBarStyleBlack;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Currently Playing..."];
[navBar pushNavigationItem:navItem animated:NO];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack:)];
navItem.rightBarButtonItem = editButton;
[self.view addSubview:navBar];
[editButton release];
[navItem release];
[navBar release];
[super viewDidLoad];
}
It's terrible i have to add an entire navbar to a UINavigationController that already has a navbar....if i try to use the existing, i get this error:
'NSInternalInconsistencyException', reason: 'Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
....really???
navigationItem must not be set on the UINavigationController instance but on the view controller of the view which is displayed "inside" the navigation controller.
Setting self.navigationItem on your navigation controller would work if your controller was itself pushed into another navigation controller.