When user tap on textfield so is there any way to open new viewController.
For example, textFieldCountryCode having already value set. now user try to change text at that time new view should be open and after select the option that view should be closed and control comes back to previous viewController with selected value should be set inside textFieldCountryCode.
You should use pickerview to pick value instead of new viewcontroller in this kind of functionality!
You can set pickerview as inputview of your textfield.
Then also if you want to use seperate viewcontroller then in textFieldDidBeginEditing method you can present that viewcontroller and you can use delegate and protocol to set value of textfield which is choosen from viewcontroller!
You can do using passing data using delegate while select country from CountryViecController.
try this code snippet
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 100) {
ViewController *videoVC = [self.storyboard instantiateViewControllerWithIdentifier:#"videoVC"];
videoVC.delegate = self;
[self.navigationController pushViewController:videoVC animated:YES];
return NO;
}
return YES;
}
// delegate method for video controller, here you get selected country
-(void)selectedString:(NSString *)selectedCountry{
NSLog(#"%#",selectedCountry);
}
This kind of functionality can be done by two approach :
First : via pickerview
You need to add your_textfield.inputView = your_pickerview;
and you can done it by handling it by delegate methods of pickerview.
Second : via TextField (Your recommended)
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(textField == Your_textfield)
{
Your_viewcontroller *vc= [self.storyboard instantiateViewControllerWithIdentifier:#"Your_viewcontroller_identifier"];
[self.navigationController pushViewController:vc animated:YES];
return NO;
}
return YES;
}
Add the textField and use the following Code:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:#""])
{
return YES;
}
else
{
NSLog (#"Go To View Controller");
[self changeView];
return NO;
}
}
-(void)changeView
{
secondScreen *obj =[[secondScreen alloc]init];
[self.navigationController pushViewController:obj animated:YES];
}
This code would move you to your second View if the textField already contains text.
In your second view you can define a button like this and set its action to a function as below to return to your previous ViewController
-(void) crossButtonPressed{
[self.navigationController popViewControllerAnimated:YES];
}
I hope this helps you out.
Yes you can push to nextViewController by using textFieldDidBeginEditing method see below code:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[_myTextfield resignFirstResponder];
TestVc *myVc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"TestVc"];
[self.navigationController pushViewController:myVc animated:YES];
}
Related
I have created a screen containing two table views where i have successfully called the method cellForRowAtIndexPath but now am facing trouble in navigating to the other page from tableview option by using didSelectRowAtIndexPath.I think am doing some mistake and i have clearly no idea what is the next step.Can anyone guide how should i do it?? I have used this code-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView isEqual:leftTableView]) {
TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooSinglesScreen"];
EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:#"EyecatcherScreen"];
TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooToplist"];
int i=indexPath.row;
if(i==0){
[self.navigationController pushViewController:obj animated:nil];
}
else if (i==1) {
[self.navigationController pushViewController:obj1 animated:NO];
}
else if (i==2) {
[self.navigationController pushViewController:obj2 animated:NO];
}
}
else {
TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooSinglesScreen"];
EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:#"EyecatcherScreen"];
TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooToplist"];
int i=indexPath.row;
if(i==0){
[self.navigationController pushViewController:obj animated:nil];
}
else if (i==1) {
[self.navigationController pushViewController:obj1 animated:NO];
}
else if (i==2) {
[self.navigationController pushViewController:obj2 animated:NO];
}
}
}
If you want to navigate from table view to another view controller don't set animated:NO.set animated:YES for navigating.But if you set animated:NO,definitely it does not navigate.So you have to do following things for navigating.
YourViewController *yourVC = (YourViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"StoryBordIdentifier"];
[self.navigationController pushViewController:yourVC animated:YES];
Also You can use in this didSelectRowAtIndexPath
[self performSegueWithIdentifier:#"segueID" sender:self];
with
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
YourViewController *vc = segue.destinationViewController;
}
Click Table Row and Go another Page
Segue or didSelectRowAtIndexPath
Making Push Segue when table row is selected
I see user3182143's answer helped, but for what it's worth I've set up a tiny demo project (now updated with two tables) to show anybody interested how it's done.
It also illustrates (as the name implies), that you can, indeed, push manually and set the animation parameter to NO, just run it in the simulator.
I can't tell why that didn't work in your original approach, pri, but my guess is that you set up your navigation view controller wrongly somehow.
For code & storyboard readability I'd recommend using segues anyways.
In my story board I have:
NavController-> View1(root) -> View2 ->View3 ->View4
I can normally use the flow in that order and navigation between segues is easy.
But How to go from View1 to View4 .. and from View4 there is back button should take you to View3?
Because if I made direct Segue From View1 to View4 ,, I cant reach View3.
Please let me know how to do this. Thanks in advance.
Here is an example with 3 viewcontrollers only but it will be the same logic:
You need to create segue between viewcontrollers like this:
viewController1 --- segueFrom1To2 ----- viewController2
viewController1 --- segueFrom1To3 ----- viewController3
viewController2 --- segueFrom2To3 ----- viewController3
Then you need a property in your viewController2, let's say: goToNextVc;
So, in your viewController1, you will be able to set this property when your perform the appropriate segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
ViewController2 *vc2 = segue.destinationViewController;
if ([segue.identifier isEqualToString:#"segueFrom1To3"])
{
vc2.goToNextVc = YES;
}
else{
vc2.goToNextVc = NO;
}
}
And now in the viewWillAppear of viewController2, simply add:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.goToNextVc){
self.goToNextVc = NO;
[self performSegueWithIdentifier:#"segueFrom2To3" sender:self];
}
}
If you don't want to see the transition between viewController1 and viewController2, simply create a custom segue, let's say SegueNoTransition in which you need to override perform:
-(void) perform{
[[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO];
}
And replace the type "show" of segueFrom1To3 with "custom" and the class with SegueNoTransition;
for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[yourViewController class]]) {
[self.navigationController popToViewController:controller
animated:YES];
}
else {
NSLog(#"Not Found");
}
}
// You must import the viewController where you want to move.
// yourViewController = viewController where you want to go in example.
(This an extension of iOS 7: Keyboard not showing after leaving modal ViewController)
I've got a HomeViewController that uses a NavigationController. Clicking a button takes you to ModalViewController using a modal segue. Pressing the back button then takes you back to the HomeViewController, which then pops up a keyboard. The weird part though is that the keyboard never shows up. I've verified that the UIKeyboardDidShowNotification does get fired , UIKeyboardDidHideNotification doesn't and [textfield isFirstResponder] returns true. Which means the keyboard should be shown right??
I've verified that this only occurs if I have a modal segue, and the NavigationController::shouldAutorotate returns NO.
I've copied the relevant code below. Any help would be greatly appreciated!!
NavigationController.m:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
HomeViewController.m:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (firstTimeComplete) {
UITextField *textField = [[UITextField alloc] init];
[self.view addSubview:textField];
[textField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:3];
}
}
ModalViewController.m:
- (IBAction)back:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Fixed! The problem was because I was using UIInterfaceOrientationPortrait instead of UIInterfaceOrientationMaskPortrait. I think masks are the expected type for supportedInterfaceOrientations
I have a UIView named scoreView on ViewController. When I load the ViewController I hide the scoreView in the viewDidLoad method. It works fine.
- (void)viewDidLoad{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
scoreView.hidden = YES;
}
Then after being pushed from another view controller to this ViewController, I call a method, where I want to show the scoreView. But the scorView is still being hidden. Where is the mistake I am doing?
-(void)levelCompleteViewAppear: (NSString *)score{
NSLog(#"This method is called!");
scoreView.hidden = NO ;
[gameScore setText:score];
}
This is how I am pushing to the ViewController from anothe view controller.
-(void)levelComplete{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[self.navigationController pushViewController:vc animated:YES];
[vc levelCompleteViewAppear:scoreLabel.text];
}
Try This,
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
}
When you are pushing the viewController to navigationController, it will not call viewDidLoad directly. If you want to make it visible, write it down in viewWillAppear or vieDidAppear method.
- (void) viewWillAppear{
scoreView.hidden = YES;
}
add this in your code
If first time ViewController class in opeining then ViewDidLoad will call but if you are coming back from some other ViewController then ViewDidLoad wont get call for that you need to show your View in ViewWillAppear Or ViewDidAppear.
Suppose you called Class A then ViewDidLoad will get call then again you moved class B then class B viewDidLoad will get call but if you are coming back again to class A then your viewDidLoad wont get call, it will call ViewWillAppear and viewDidAppear.
Hope it may clear you more.
-(void)viewWillApper:(BOOL)animated{
scoreView.hidden = NO ;
}
I am trying to get a UITableView to show up after I tap a next button, I can't get it to work...
I don't know why TimesViewController.m isn't working, how do I link it...
-(IBAction)nextView:(UIBarButtonItem *) sender{
[self pushViewController:TimesTableViewController.m animated:YES];
}
You need to read through View Controller programming guide. Essentially, it depends more on your application design. The most common ways of doing this are:
Tap on nextView should present TableView controller modally.
Tap on nextView should push TableView controller in the navigation stack.
With either of the approach, you need to create a separate view controller for your table view which will manage the table view and act as delegate/data source for it.
From within your button action handler you need to do it this way:
-(IBAction)nextView:(UIBarButtonItem *) sender{
MyTableViewController *tableController = [[MyTableViewController alloc] init];
// For #1 above
[self presentViewController:tableController animated:YES completion:^{
// Any code that you want to execute once modal presentation is done
}];
// For #2 above
[self.navigationController pushViewController:tableController animated:YES];
}
EDIT:
Create a initialize method on your MyTableViewController and pass the values while calling it.
- (id)initWithData1:(NSString *)iData1 data2:(NSString *)iData2 {
if ((self = [super init])) {
self.data1 = iData1;
self.data2 = iData2;
}
return self;
}
MyTableViewController *tableController = [[MyTableViewController alloc] initWithData1:#"Some String" data2:#"Another string"];
PS: You could also expose the string property in your header file of MyTableViewController and set it from the callee class
or if you wanna do it programmatically, in your viewDidLoad, after you created your button
[firstsessionButton addTarget:self action:#selector(showtable:) forControlEvents:UIControlEventTouchUpInside];
-(void)showtable:(UIButton *)sender{
TableViewController *tableViewController=[[TableViewController alloc]init];
[self.navigationController pushViewController:tableViewController animated:YES];
}