【问题标题】:How can i display the result of a method in a new view?如何在新视图中显示方法的结果?
【发布时间】:2012-01-05 10:35:47
【问题描述】:

我创建了一个以登录页面开头的应用程序,在输入一些用户信息后,我向服务器发送了一个 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!");
}
}

【问题讨论】:

    标签: objective-c uiview uiviewcontroller ios5


    【解决方案1】:

    以下是您的疑问的答案,可能会对您有所帮助- 1. 我还想创建一个新视图并在该视图上创建按钮。 为此,您可以创建一个新的视图控制器并在收到响应后推送该视图控制器。您也可以将响应从一个控制器浮动到另一个控制器。 2.第二个问题,使用此代码,我的按钮只是向下移动,但我想要两行按钮 您需要正确设置按钮的框架,然后才能完美地工作。

    【讨论】:

    • 首先感谢本指南,但正如我在问题的第一句话中指出的那样,我对 Objective C 的了解并不多,您能简单地说一下我必须使用哪些类吗?或者任何指向相关问题教程的链接都会很棒..再次感谢
    • 你可以google一下,那里有很多教程。比如-edumobile.org/iphone/iphone-programming-tutorials/…
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多