如何在新视图中显示方法的结果? [英] How can i display the result of a method in a new view?
问题描述
我创建了一个以登录页面开头的应用程序,在输入一些用户信息后,我将GET发送到服务器。使用响应值我动态创建按钮,标题等。一切都很好,但在我发送GET并使用JSON响应后,我的按钮正在使用登录页面在同一视图上创建。我还想在该视图上创建一个新视图并创建按钮。由于我使用了一些来自用户信息的参数,我需要使用我的MainViewController在同一个类中创建视图(在我的例子中它是 BNT_1ViewController
)但我无法弄清楚如何处理这种情况。我也在共享代码,也许它会有所帮助。
第二个问题,使用此代码我的按钮只是向下移动但我需要两行按钮。如何更改x轴上按钮的位置?我更改了这些值,但整个列都发生了变化,即使我只想将两个按钮放在同一行中,所有结构都会替换..
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!");
}
}
推荐答案
关注是您的查询的答案,可能会帮助您 -
1.我还想创建一个新视图并在该视图上创建按钮。
为此你可以创建一个新的视图控制器并在收到响应后推送该视图控制器。您也可以将响应从一个控制器浮动到另一个控制器。
2.还有第二个问题,使用此代码我的按钮只是向下移动但是我需要两行按钮
你需要正确设置按钮框,然后才能完美地工作。
following are answers of your queries, might be it will help you- 1. I want to also be creating a new view and create buttons on that view. for this you can create a new view controller and push that view controller once you received the response. Also you can float the response from one controller to other. 2. And a second question, with this code my buttons just move downwards but i want two lines of buttons You need to set frames of buttons properly, then will work perfectly.
这篇关于如何在新视图中显示方法的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!