我创建了一个以登录页面开头的应用程序,在输入一些用户信息后,我将GET发送到服务器。使用响应值我动态创建按钮,标题等。一切都很好,但在我发送GET并使用JSON响应后,我的按钮正在使用登录页面在同一视图上创建。我还想在该视图上创建一个新视图并创建按钮。由于我使用了一些来自用户信息的参数,我需要使用我的MainViewController在同一个类中创建视图(在我的例子中它是BNT_1ViewController
),但我无法弄清楚如何处理这种情况。我也在分享代码,也许有帮助。
第二个问题,使用此代码我的按钮只是向下移动,但我想要两行按钮。如何更改x轴上按钮的位置?我更改了这些值,但整个列都发生了变化,即使我只想在同一行中放置两个按钮,所有结构都会替换..
-(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!");
}
}
答案 0 :(得分:1)
以下是您的疑问的答案,可能会对您有所帮助 - 我还想创建一个新视图并在该视图上创建按钮。 为此,您可以创建一个新的视图控制器,并在收到响应后推送该视图控制器。您也可以将响应从一个控制器浮动到另一个控制器。 2.第二个问题,使用此代码我的按钮只是向下移动,但我想要两行按钮 你需要正确设置按钮框,然后才能完美地工作。