I have created an app which starts with a login page, after entering some user information I send a GET to server. Using the response values I create buttons, their title etc dynamically. Everything is ok, but after I send GET and use the JSON response my buttons are being created on the same view with the login page. I want to also create a new view and create buttons on that view. Since I use some parameters from user information I need to create the view in the same class with my MainViewController (in my case it is BNT_1ViewController
) but I can't figure out how to handle this situation. I'm sharing the code too, maybe it helps.
And a second question, with this code my buttons just move downwards but I want two lines of buttons. How can I change the position of a button in the x axis? I changed those values but the whole column changes, even though I want just to place two buttons in the same line, all the struct replaces..
-(IBAction)Accept:(id)sender
{ userName=[[NSString alloc] initWithString:userNameField.text ];
[userNameField setText:userName];
NSUserDefaults *userNameDef= [NSUserDefaults standardUserDefaults];
[userNameDef setObject:userName forKey:@"userNameKey"];
password =[[NSString alloc] initWithString:passwordField.text];
[passwordField setText:password];
NSUserDefaults *passDef=[NSUserDefaults standardUserDefaults];
[passDef setObject:password forKey:@"passwordKey"];
serverIP=[[NSString alloc] initWithString: serverField.text];
[serverField setText:serverIP];
NSUserDefaults *serverDef=[NSUserDefaults standardUserDefaults];
[serverDef setObject:serverIP forKey:@"serverIPKey"];
[userNameDef synchronize];
[serverDef synchronize];
[passDef synchronize];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"BNTPRO "
message:@"Your User Informations are going to be sent to server. Do you accept?"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel", nil];
[message show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"OK"])
{
if([userNameField.text isEqualToString:@"" ]|| [passwordField.text isEqualToString:@""] || [serverField.text length]<10)
{
UIAlertView *message1 = [[UIAlertView alloc] initWithTitle:@"BNTPRO "
message:@"Your User Informations are not defined properly!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[message1 show];
[userNameField resignFirstResponder];
[passwordField resignFirstResponder];
return;
}
//## GET code to here**
NSString *str1=[@"?username=" stringByAppendingString:userNameField.text];
NSString *str2=[@"&password=" stringByAppendingString:passwordField.text];
NSString *str3=[str1 stringByAppendingString:str2];
NSString *str4 =[@"http://" stringByAppendingString:serverField.text];
NSURL *url=[NSURL URLWithString:[str4 stringByAppendingString:[@"/ipad/login.php" stringByAppendingString:str3]]];
NSLog(@"%@\n",url);
//get the url to jsondata
NSData *jSonData=[NSData dataWithContentsOfURL:url];
if (jSonData!=nil) {
NSError *error=nil;
id result=[NSJSONSerialization JSONObjectWithData:jSonData options:
NSJSONReadingMutableContainers error:&error];
if (error==nil) {
NSDictionary *mess=[result objectForKey:@"message"];
NSDictionary *messContent=[mess valueForKeyPath:@"message"];
NSDictionary *messDate=[mess valueForKeyPath:@"date"];
NSDictionary *messID=[mess valueForKeyPath:@"ID"];
NSLog(@"%@ *** Message %@ \n Message Content: %@ \n Mesage ID: %@ \n Message Date: %@", result, mess, messContent, messID,messDate);
NSString*key1=[ result objectForKey:@"key" ];
NSString *s1=[@"http://" stringByAppendingString:serverField.text];
NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"];
NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]];
NSLog(@"\n%@\n",url2 );
NSData *data2=[NSData dataWithContentsOfURL:url2];
id result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
//i create buttons from here
self.buttons = [NSMutableArray array];
CGFloat yPosition = 43.0f;//measure from top point
CGFloat xPosition = 34.0f;
const CGFloat buttonHeight = 70.0f;//height
const CGFloat buttonMargin = 32.0f;//distance between two buttons
for(NSDictionary* buttonData in result2) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* buttonTitle = [buttonData objectForKey:@"name"];
[button addTarget:self action:@selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor cyanColor]];
[button setTitle:buttonTitle forState:UIControlStateNormal];
// button.frame = CGRectMake(20.0f, yPosition, 130.0f, buttonHeight);//if([count]%2==1)
NSLog(@"Ne oluyor?= %d",[buttonData count]);
button.frame = CGRectMake(170.0f, yPosition, 130.0f, buttonHeight);//if([count]%2==1)
[button addTarget:self action:@selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.buttons addObject:button];
yPosition+= buttonHeight + buttonMargin;
} }
}
else if([title isEqualToString:@"Cancel"])
{
NSLog(@"You changed your mind!");
}
}