There are two pages one for registration and other for login. username and password in login page should match with data in registration page. To do this i am using if-else statement for button that if data in registration page== login page username and password then go to next page on button click otherwise display error.
This is .h file
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController<UITextFieldDelegate>
#property(strong,nonatomic)NSString *dataString;
#property (strong, nonatomic) IBOutlet UITextField *inputTxt1;
#property (strong, nonatomic) IBOutlet UITextField *inputTxt2;
#property (strong, nonatomic) IBOutlet UILabel *lblOutput;
- (IBAction)btnAction:(id)sender;
#end
this is .m file
#import "ViewController2.h"
#interface ViewController2 ()
{
int x;
}
#end
#implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title=#"Login Id Page";
NSLog(#"Data String is %#",self.dataString);
_inputTxt1.delegate=self;
_inputTxt2.delegate=self;
_inputTxt1.returnKeyType=UIReturnKeyNext;
_inputTxt2.returnKeyType=UIReturnKeyDone;
if (_inputTxt1.text == self.dataString){
x=1;
}
else{
x=0;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.inputTxt1) {
[self.inputTxt2 becomeFirstResponder];
}
else if (textField == self.inputTxt2)
{
[textField resignFirstResponder];
}
return YES;
}
//error is comming right here at if statement:-
if (x==1)
{
- (IBAction)btnAction:(id)sender {
//navigation to next page
}
}
else
{
_lblOutput.text = #"wrong username or password";
}
#end
do like
//error is comming right here at if statement:-
if (x==1)
{
[self performseguewithIdentifier:#"xxxx"];
}
else
{
_lblOutput.text = #"wrong username or password";
}
choice-2
if (x==1)
{
[self btnAction:nil];
}
else
{
_lblOutput.text = #"wrong username or password";
}
cal method like
- (IBAction)btnAction:(id)sender {
//navigation to next page
[self performseguewithIdentifier:#"xxxx"];
// or do ur stuff here
}
first you define
- (IBAction)btnAction:(id)sender
{
//navigation to next page
}
then call it
if (x==1)
{
[self btnAction:nil]; //here
}
Related
I want to hide keyboard if user press return key!
here .h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>;
#property (weak, nonatomic) IBOutlet UITextField *testItHere;
#end
and .m
#implementation ViewController
#synthesize testItHere;
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
{
[self.testItHere.resignFirstResponder]
return YES
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self updateTestKeyboard:self.interfaceOrientation];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.testItHere.delegate=self;
}
But error says:expected identifier, what is wrong with this code?
Remove the dot:
[self.testItHere resignFirstResponder];
Remove dot and you call textFieldShouldReturn for hide keyboard when return key is pressed.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.testItHere resignFirstResponder];
return YES;
}
I have the declared variable:
#property(strong, nonatomic) NSString* like_id;
I call function GetLike inside - (void)viewDidLoad {}
- (void) GetLike {
if(![self.like_id isEqualToString:#""]){
// TODO
}
}
I get error: Thread 1, signal SIGABRT
This works fine for me.
#interface ViewController ()
#property(strong, nonatomic) NSString* like_id;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self getLike];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)getLike {
if(![self.like_id isEqualToString:#""]){
NSLog(#"Liked !");
}
}
#end
I am having problems trying to get the keyboard to go to the next field or return when the return key is pressed, my code is:
#interface LoginViewController ()
#property (weak, nonatomic) IBOutlet UIButton *loginButton;
#property (weak, nonatomic) IBOutlet UITextField *userField;
#property (weak, nonatomic) IBOutlet UITextField *passwordField;
#property (strong, nonatomic) UITextField *textField;
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidLayoutSubviews
{
[self.userField becomeFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)tapLogin:(id)sender {
NSLog(#"login");
}
#pragma mark - UITextFieldDelegate methods
// for return and next
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.userField) {
[self.passwordField becomeFirstResponder];
}
else if (textField == self.passwordField) {
[self tapLogin:nil];
}
return YES;
}
The cursor beings in the userField but hitting the return key does nothing, if I manually tap on another field i.e the password one this also won't do anything. The cursor is stuck in the username field. I have implemented the protocol in my header file and also set the text fields as delegates in the interface builder. Could someone help please?
Try this,
Remove [self.userField becomeFirstResponder]; method from viewDidLayoutSubviews ie, remove the below method
- (void)viewDidLayoutSubviews
{
[self.userField becomeFirstResponder];
}
and add
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.userField becomeFirstResponder];
}
I have this code on my textFieldShouldReturn (is not in the same class as the method called):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
LoginViewController *loginViewController = [[LoginViewController alloc]init];
if (textField.returnKeyType == UIReturnKeyJoin) [loginViewController logIn];
return (textField.returnKeyType == UIReturnKeyDone);
}
Everything works perfect until this point, this is the code of the "login" method:
- (IBAction)logIn{
NSString *username = [[self.usernameLoginField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
NSString *password = [[self.passwordLoginField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
NSLog(#"Username: %# - Password: %# ",username,password);
//Whatever
}
On my view, I have a button that calls that method, when I use that button the NSLog shows what the UITextField contains in that moment, otherwise, If the method is called from the "Join" (Return key) from de keyboard, the NSLog shows null content on the variables.
What am I missing?.
In my opinion you have 3 possible solutions:
Option 1: The best option IMHO.
You can use UIAlertView with UIAlertViewStyle = UIAlertViewStyleLoginAndPasswordInput
self.alertView = [[UIAlertView alloc] initWithTitle:nil
message:#"LogIn"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Cancel", #"Join", nil];
self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[self.alertView show];
Then with the delegate:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == self.alertView)
{
if (buttonIndex == 1)
{
[self logIn:[[alertView textFieldAtIndex:0] text] andPassword:[[alertView textFieldAtIndex:1] text]];
}
else if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
}
Remember to add: UIAlertViewDelegate
Option 2:
Working with [self performSegueWithIdentifier:#"identifier" sender:self] and
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
#pragma mark - UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.usernameLoginField)
{
[self.usernameLoginField resignFirstResponder];
[self.passwordLoginField becomeFirstResponder];
}
else if (textField == self.passwordLoginField)
{
[self.passwordLoginField resignFirstResponder];
//This Identifier is in the Storyboard
[self performSegueWithIdentifier:#"option2Segue" sender:self];
}
return true;
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[(YourViewController *)segue.destinationViewController setUsername:self.usernameLoginField.text];
[(YourViewController *)segue.destinationViewController setPassword:self.passwordLoginField.text];
}
In YourViewController.h you need:
#property (nonatomic, strong) NSString* username;
#property (nonatomic, strong) NSString* password;
Remember to add: UITextFieldDelegate
Option 3
To send back information, you should use Delegation:
In Your firstViewController.h:
#import "DelegateViewController.h"
#interface FirstViewController : UIViewController
(void) logIn: (NSString *) username password: (NSString *) password;
In Your firstViewController.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"option3Delegate"])
{
((DelegateViewController *) segue.destinationViewController).delegate = self;
}
}
In Your delegateViewController.h:
#protocol Option3Delegate <NSObject>
- (void) logIn: (NSString *) username password: (NSString *) password;
#end
#interface DelegateViewController : UIViewController
{
id myDelegate;
}
#property (nonatomic, assign) id<Option3Delegate> delegate;
#end
In Your delegateViewController.m:
#pragma mark - UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.usernameLoginField)
{
[self.usernameLoginField resignFirstResponder];
[self.passwordLoginField becomeFirstResponder];
}
else if (textField == self.passwordLoginField)
{
[self.passwordLoginField resignFirstResponder];
//This Identifier is in the Storyboard
if ([_delegate respondsToSelector:#selector(logIn:password:)])
{
[_delegate logIn:self.usernameLoginField.text password:self.passwordLoginField.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
return true;
}
Remember to add: UITextFieldDelegate
Option 4: With KeyboardController
KeyboardController is a small solution to handle the Keyboard interaction inside UITextFields. However only handle with Next and Done Return keys. To handle the Join key you can do it in your ViewController. In the view controller where you allocate KeyboardController:
#property (strong, nonatomic) IBOutlet UITextField *usernameLoginField;
#property (strong, nonatomic) IBOutlet UITextField *passwordLoginField;
#property (strong, nonatomic) KeyboardController *keyboardController;
- (void)viewDidLoad
{
[super viewDidLoad];
id fields = #[self.usernameLoginField, self.passwordLoginField];
self.keyboardController = [KeyboardController controllerWithFields:fields];
//Important
self.passwordLoginField.delegate = self;
}
#pragma mark - UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.passwordLoginField)
{
[self.passwordLoginField resignFirstResponder];
[self logIn];
}
return true;
}
- (IBAction)logIn
{
NSLog(#"Username: %# - Password: %#", self.usernameLoginField.text, self.passwordLoginField.text);
//Whatever
}
You can download an example with the 4 options here:
I have watched tutorials and what I have learnt, that I have implemented on a project with two viewControllers just a simple firstName, lastName practice but I don't know where to place the method setFirstName: in a facebook project that I'm doing I wish to login (that works) then it automatically goes to another viewController (that works) I just cant get the user.name to be displayed, in which facebook delegate do i call the method, I fetch the info here and place it in the property in the loginViewController
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
firstName = user.name;
[[self delegate] setFirstName:firstName];
}
then in the profileViewController I place this:
- (void)setFirstName:(NSString *)firstName
{
firstNameString = firstName;
}
then i assign the properties here
-(void)viewWillAppear:(BOOL)animated
{
self.firstNameLabel.text = firstNameString;
}
i think my issue is when I'm pushing to the profileViewController I'm not passing the info in the transition cause i don't use a segue I'm just pushing the viewController please help been on this issue for a week or so thanks
EDIT::
LoginViewController.m
// this method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
self.profilePictureView.profileID = user.id;
FBNamePass = user.name;
}
- (void)pushViewController
{
NeXtViewController *controller = [[NeXtViewController alloc] init];
controller.FBNameString = FBNamePass;
[self.navigationController pushViewController:controller animated:YES];
}
EDIT 2::
LoginViewController.h
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#protocol passNames <NSObject>
- (void)setFBName: (NSString *)FBName;
#end
#interface ViewController : UIViewController <FBLoginViewDelegate>
#property (retain) id <passNames> delegate;
#property (strong, nonatomic) NSString *FBNamePass;
#end
LoginViewController.m
#import "ViewController.h"
#import "NeXtViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UILabel *statusLabel;
#property (strong, nonatomic) IBOutlet UILabel *nameLabel;
#property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePictureView;
#end
#implementation ViewController
#synthesize delegate, FBNamePass;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a FBLoginView to log the user in with basic, email and likes permissions
// you should always ask for basic permissions when loggin the user in
FBLoginView *loginView = [[FBLoginView alloc] initWithReadPermissions:#[#"basic_info",#"email",#"user_likes"]];
// set this loginUIViewCOntroller to be the loginView button's delegate
loginView.delegate = self;
// align the button in the center horizontally
loginView.frame = CGRectMake(25, 299, 271, 50);
// align the button in the center vertically
//loginView.center = self.view.center;
// add the button to the view
[self.view addSubview:loginView];
[self pushViewController];
}
-(void)viewDidAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// this method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
self.profilePictureView.profileID = user.id;
FBNamePass = user.name;
}
// implement the loginViewShowingLoggedInUser: delegate method to modify your app's UI for a logged-in user experoience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
self.statusLabel.text = #"Logged in";
if ([self.statusLabel.text isEqualToString:#"Logged in"]) {
NeXtViewController *n = [self.storyboard instantiateViewControllerWithIdentifier:#"NeXt"];
[self.navigationController pushViewController:n animated:NO];
}
}
// implement the loginViewShowingLoggedOutUser: delegate method to modify your app's UI for a logged-out user experoience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
self.profilePictureView.profileID = nil;
self.nameLabel.text = #"";
self.statusLabel.text = #"You're not logged in";
}
// You need to override loginView:handleError in order to handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error
{
NSString *alertMessage, *alertTitle;
// If the user should perform an action outside of you app to recover,
// the SDK will provide a message for the user, you just need to surface it.
// This conveniently handles cases like Facebook password change or unverified Facebook accounts.
if ([FBErrorUtility shouldNotifyUserForError:error]) {
alertMessage = [FBErrorUtility userMessageForError:error];
alertTitle = #"Facebook Error";
// This code will handle session closures since that happen outside of the app.
// You can take a look at our error handling guide to know more about it
// https://developers.facebook.com/docs/ios/errors
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
alertTitle = #"Session Error";
alertMessage = #"Your current session is no longer valid. Please log in again.";
// If the user has cancelled a login, we will do nothing.
// You can also choose to show the user a message if cancelling login will result in
// the user not being able to complete a task they had initiated in your app
// (like accessing FB-stored information or posting to Facebook)
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(#"user cancelled login");
// For simplicity, this sample handles other errors with a generic message
// You can checkout our error handling guide for more detailed information
// https://developers.facebook.com/docs/ios/errors
} else {
alertTitle = #"Something went wrong";
alertMessage = #"Please try again later";
NSLog(#"Unexpected error:%#",error);
}
if (alertMessage) {
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles:nil] show];
}
}
- (void)pushViewController
{
NeXtViewController *controller = [[NeXtViewController alloc] init];
controller.FBNameString = FBNamePass;
[self.navigationController pushViewController:controller animated:YES];
}
ProfileViewController.h
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import "ViewController.h"
#interface NeXtViewController : UIViewController <passNames>
{
ViewController *view;
}
#property (strong,nonatomic) NSString *FBNameString;
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
#end
ProfileViewController.m
#import "NeXtViewController.h"
#interface NeXtViewController ()
#end
#implementation NeXtViewController
#synthesize FBNameString;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
view = [[ViewController alloc] init];
[view setDelegate:self];
self.nameLabel.text = FBNameString;
self.navigationItem.hidesBackButton = YES;
}
-(void)viewDidAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setFBName:(NSString *)FBName
{
FBNameString = FBName;
}
#end
You need create property in ProfileViewController -> create instance of ProfileViewController -> set user name to instance property -> push instance of ProfileViewController:
LoginViewController.m
- (void)pushProfileViewController {
ProfileViewController *controller = [[ProfileViewController alloc] init];
controller.firstName = firstNameString;
[self.navigationController pushViewController:controller animated:YES];
}