Log in

View Full Version : Objective C Help



Jim
07-05-2012, 08:05 PM
I need help with Objective C. In this case I've created a UIAlertView with UIAlertViewStyleLoginAndPasswordInput style and I want to be able to check if the login data is correct, but I don't know how to check it after a button has been pressed.


- (IBAction)loginPushed:(id)sender
{
UIAlertView *loginAlert = [[UIAlertView alloc]initWithTitle:@"Login to BindingDB" message:@"Enter your login info" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleLoginAndPassword Input];
[loginAlert addButtonWithTitle:@"Enter"];
[loginAlert addButtonWithTitle:@"Cancel"];
[loginAlert show];
[self checkLogin:loginAlert];
}

- (void)checkLogin:(UIAlertView *)loginAlert
{
//I don't know what to put in this if statement to make it work only when the "enter" button has been pressed
if () {
NSString *login = [[loginAlert textFieldAtIndex:0] text];
NSString *password = [[loginAlert textFieldAtIndex:1] text];
if (login == @"Jim" && password == @"Password") {
[self verifyLogin:true];
}
else {
[self verifyLogin:false];
}
}
}

- (void)verifyLogin:(BOOL)checkLogin {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
if (checkLogin) {
[alert setTitle:@"Login Successful"];
[alert setMessage:@"You have logged in"];
}
else {
[alert setTitle:@"Login Failed"];
[alert setMessage:@"Try Again"];
}

[alert show];
}

Silentcore
07-05-2012, 08:09 PM
why not post it here so people who take a look at this thread, can see the code and see it and that might help you out a bit more then just reading words..

Jim
07-05-2012, 08:11 PM
why not post it here so people who take a look at this thread, can see the code and see it and that might help you out a bit more then just reading words..

Objective C isn't that popular of a language, don't know if too many SRL people would know it.

Silentcore
07-05-2012, 08:12 PM
Objective C isn't that popular of a language, don't know if too many SRL people would know it.

and you wont get any help if you dont post here..you never know if someone takes a look at the thread and decides to help you out bro..

Brandon
07-05-2012, 08:42 PM
Why is everything commented out :S? Use CodeTags or do:

[ Highlight=C]
[ /Highlight]

Remove the spaces in the tags.

Jim
07-05-2012, 08:54 PM
Why is everything commented out :S? Use CodeTags or do:

[ Highlight=C]
[ /Highlight]

Remove the spaces in the tags.

Ok I changed it. Could you help me with this?

Brandon
07-05-2012, 09:13 PM
Well I won't be able to full write all the code for you since I no longer have my mac or hackintosh (Testing Windows 8 atm).

But I can tell you that you need to check which sender is giving you the msg.

Delegate UIAlertview and iit's implementation should be:


- (void)checkLogin:(UIAlertView *)loginAlert clickedButtonAtIndex:(NSInteger)BtnIndex
{
if (BtnIndex == 0) //You can also use a switch case here and switch on whatever button index.
{
//Validate the login.
}
else if (BtnIndex == 1)
{
//Cancel login.
}
}

TBH this is busting my balls right now lol. It's been forever!! Since I touched iPhone development. I honestly hope that helps; Sat here for a good 3-5 mins thinking hard.. Really hard without a compiler to test.

Jim
07-05-2012, 09:47 PM
Well I won't be able to full write all the code for you since I no longer have my mac or hackintosh (Testing Windows 8 atm).

But I can tell you that you need to check which sender is giving you the msg.

Delegate UIAlertview and iit's implementation should be:


- (void)checkLogin:(UIAlertView *)loginAlert clickedButtonAtIndex:(NSInteger)BtnIndex
{
if (BtnIndex == 0) //You can also use a switch case here and switch on whatever button index.
{
//Validate the login.
}
else if (BtnIndex == 1)
{
//Cancel login.
}
}

TBH this is busting my balls right now lol. It's been forever!! Since I touched iPhone development. I honestly hope that helps; Sat here for a good 3-5 mins thinking hard.. Really hard without a compiler to test.

Thanks, I did what you did and delegated it, and it almost works, I'm just gonna try some new stuff to make it so it only applies to that alert view and not any others I create.

Tniffoc
07-05-2012, 10:09 PM
I think this is the protocol that you're looking for. This should tell you everything that you needed to know. I'm an iPhone dev, but most people here at SRL are not.

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/doc/uid/TP40007548