2013-03-25 60 views
1

什么是使用标签栏传输数据的标准模式?标签栏代表的标准模式是什么

+0

“连接到PHP”连接到PHP是真的只是加载网页 – 2013-03-25 23:43:26

+0

如果你发布这只是给别人以后参考,你需要把它的问题和 - 格式化并接受你自己的答案。 – woz 2013-03-25 23:44:36

+0

好的,会做的。谢谢 – 2013-03-25 23:45:40

回答

2
  1. 设置本地服务器计算机上(最终这是一个将网站上托管,等...)要做到这一点,我建议如下:

  2. 下载XAMPP(http://www.apachefriends.org/en/xampp-macosx.html)按照指示

    • 安装在应用

    • 让htcdocs在XAMPP文件夹中的应用程序读取&写所有

    • 创建一个新的文件夹名为教程(在htcdocs)
    • 添加名为index.php的
    • 编写PHP代码的新的PHP文件(注意,这是不是安全的,只有非常简单的例子)
的index.php中写

: //非常不安全的,但这个发现功能参数名

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); 
} 

- 保存并关闭您的index.php文件

  1. 在Xcode中创建一个新项目,将它设置了一个文本标签,将显示在屏幕上的结果(很简单的谷歌的教程,如果你不知道如何做到这一点)

设置

细节和步骤h和.m文件 解释(抱歉,不知道为什么这里的代码部分不显示在Xcode中

连接这样)

//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