dshgnt2008 2013-03-25 23:42
浏览 28
已采纳

tabbar委托的标准模式是什么[关闭]

what is the standard pattern for transfering data using the tabbar?

  • 写回答

1条回答 默认 最新

  • duanqinqian5299 2013-03-25 23:48
    关注
    1. Set up a local server on your computer (eventually this is what would be hosted on a website, etc...) To do this I suggest the following:

    2. Download XAMPP (http://www.apachefriends.org/en/xampp-macosx.html) follow instructions

      • Install in applications

      • Make htcdocs in XAMPP folder in applications read &write for all

      • Create a new folder titled Tutorials (in htcdocs)
      • Add a new php file called index.php
      • Write php code (NOTE THIS IS NOT SECURE, ONLY A VERY BASIC EXAMPLE)

    in index.php write: //Very unsecure, but this finds the function parameter name

    if(function_exists($_GET['f'])) {
        //If found, call the function with value as a parameter
       $_GET['f']($_GET['value']);
    }
    //actual function that takes in a number and finds the square of the number
    function getLabel($number){
        //This variable response with parameter name is equal to the number times the number    
        $response['name']=$number*$number;
        //Return the data to the caller (in JSON encoded format)
        echo json_encode($response);
    }
    

    -Save and close your index.php file

    1. In Xcode create a new project, set it up with a text label that will display the result on the screen (very simple google a tutorial if you don't know how to do this)

    Set up the connection in Xcode

    Details and steps are explained in the .h and .m file (sorry not sure why the code portion here isn't showing as such)

    //Step 1, add NSURLConnectionDataDelegate
        //.h
    @interface ViewController : UIViewController<NSURLConnectionDataDelegate>
    @property (strong, nonatomic) IBOutlet UILabel *answer;
    @end
    

    .m

    #import "ViewController.h"
    
    @interface ViewController ()
    {//step 2 local data objects
        NSMutableData*webData;
        NSURLConnection*connection;
    
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //Step 8 call (send) the request
        [self getData];
        // Do any additional setup after loading the view, typically from a nib.
    
    
        //NSDictionary*dict=[NSJSONSerialization se]
    }
    //Step 3 implement this method 
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        [webData setLength:0];
    }
    
    //Step 4 append the connection data to your object
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        [webData appendData:data];
    }
    
    //Step 5 Process results from request (called automatically when the data arrives)
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
        //Main parse
    
        //Web data
    
        NSError*error;
    
        //Dictionary serialized (processed) using JSON (Way of encoding data from a web request)
        NSDictionary*data=[NSJSONSerialization JSONObjectWithData:webData options:0 error:&error];
    
        //If data is nil, then print error
        if (data==nil) {
            NSLog(@"%@",error);
        }
    
        //Print the data
        NSLog(@"%@",data);
    
        //Get the numerical result from the dictionary key name
        NSNumber*num=[data valueForKey:@"name"];
    
        //Convert number to string
        NSString*label=[num stringValue];
    
        //Set the label to this result
        _answer.text=label;
    
    }
    //Step 7, actually initialize the request
    -(void)getData{
        //I will break this down as if it where a generic method
        //Connect to the index.php file via this URL
    
    
        //Localhost/tutorials is the XAMPP folder on your computer
    
        //index.php is the php file
    
        //getLabel(int number){}
        //?f=getLabel (calling this method in the php file)
    
        //number=900
        //&value=900 is the parameter
        NSURL*url=[NSURL URLWithString:@"http://localhost/tutorials/index.php?f=getLabel&value=900"];
    
        //In the php file it does number*number and returns the results
    
        //URL request
        NSURLRequest*request=[NSURLRequest requestWithURL:url];
    
        //Set the connection
        connection = [NSURLConnection connectionWithRequest:request delegate:self];
    
        if (connection) {
            webData=[[NSMutableData alloc]init];
        }
    
        //*****Results of this request are processed by step 5
    
    }//Step 6, in case data connection fails
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        NSLog(@"fail");
    
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料