【问题标题】:XCode iPhone database information stream by using an external *.php fileXCode iPhone 数据库信息流使用外部 *.php 文件
【发布时间】:2012-09-06 11:45:53
【问题描述】:

---我的 Xcode-ViewController.h 如下:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    IBOutlet UILabel *Anzeige;
    IBOutlet UIScrollView *scrollAnzeige;
}

-(IBAction)StartButton:(id)sender;

@end

---我的 Xcode-ViewController.m 如下(我创建/编辑的东西):

-(IBAction)StartButton:(id)sender{

    NSString *hostSTR = @"http://localhost/iphoneconnect.php";

    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostSTR]];

    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding:NSASCIIStringEncoding];

    NSString *finalString = @"";

    finalString = [serverOutput  stringByReplacingOccurrencesOfString:@"#" withString:@"\n"];

    Anzeige.text = finalString;

}

- (void)viewDidLoad
{
    [scrollAnzeige setScrollEnabled:YES];
    [scrollAnzeige setContentSize:CGSizeMake(614, 730)];//614 breit


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

---我的PHP代码如下:

<?

session_start();

$connect = mysql_connect("my.server.com", "user" , "pass") 
or die("Verbindung zur Datenbank konnte nicht hergestellt werden"); 

mysql_select_db("database") or die ("Datenbank konnte nicht ausgewählt werden"); 

$q = mysql_query("SELECT * FROM table");

while($x = mysql_fetch_assoc($q))
{
        echo $x['coloumn1']." ".$x['coloumn2']." ".$x['coloumn3']." ".$x['coloumn4']." ".$x['coloumn5']." ".$x['coloumn6']." ".$x['coloumn7']." ".$x['coloumn8']." ".$x['coloumn9']." ".$x['coloumn10']." ".$x['coloumn11']." ".$x['coloumn12']."#";
}

mysql_close($connect);

?>

一切正常 - 从数据库下载并显示信息。 我想要意识到的最重要的事情是,信息每秒都在刷新(或更高的延迟 - 取决于可能的情况)。

我使用了usleep(1000),但是我模拟器中的按钮就像是永久按下一样,根本没有显示任何信息。

之后,我尝试使用更高的延迟,例如 5 秒,但出现了同样的问题。 -> 按钮看起来像永久按下,根本没有显示任何信息。

我怎样才能解决这个问题 - 我怎样才能让它像流一样工作?

Greetz Shaddy

【问题讨论】:

    标签: php iphone database xcode stream


    【解决方案1】:

    usleep 正在阻塞并暂停代码的执行。

    要每秒刷新一次数据,请使用 +timerWithTimeInterval:target:selector:userInfo:repeats: 方法实例化 NSTimer 并将其添加到主运行循环中。

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0f
                  target:self
                  selector:@selector(refreshData:)
                  userInfo:nil
                  repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:@"NSDefaultRunLoopMode"];
    

    然后将请求代码移动到方法refreshData:(NSTimer*)timer。要停止计时器,您必须调用 [timer invalidate]

    因此,如果您的上述代码有效,则以下代码应具有预期的效果:

    -(void)refreshData:(NSTimer*)timer
    {
        NSString *hostSTR = @"http://localhost/iphoneconnect.php";
    
        NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostSTR]];
    
        NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding:NSASCIIStringEncoding];
    
        NSString *finalString = @"";
    
        finalString = [serverOutput  stringByReplacingOccurrencesOfString:@"#" withString:@"\n"];
    
        Anzeige.text = finalString;
    
    }
    
    
    -(IBAction)StartButton:(id)sender
    {
        UIButton* button = (UIButton*)sender;
        if (!timer){
            timer = [NSTimer timerWithTimeInterval:1.0f
                                                     target:self
                                                   selector:@selector(refreshData:)
                                                   userInfo:nil
                                                    repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:timer forMode:@"NSDefaultRunLoopMode"];
            [button setTitle:@"Stop" forState:UIControlStateNormal];
        }else{
            [timer invalidate];
            timer = nil;
            [button setTitle:@"Start" forState:UIControlStateNormal];
        }
    }
    

    别忘了将NSTimer *timer; 添加到您的界面。

    最好的,彼得

    【讨论】:

    • 首先,我要感谢您快速而称职的回答!我试过了,但我失败了。我会解释它.. 现在我宣布“NSTimer *timer;”在 ViewController:UIViewController 接口下的 ViewController.h 中,但我无法实例化它。主运行循环在哪里?我尝试了不同的方法将您的代码添加到我的方法中。我对所有这些东西都不熟悉,所以请原谅。
    • 好的。您应该习惯于使用在线的参考手册。如果您输入某物,Google 通常会在第一次点击时指向手册。像 NSRunLoop 或 NSTimer。如果您仍然无法弄清楚,我可以稍后提供更多代码示例。彼得
    • 非常感谢!它正在工作。 :-) 我现在要练习一下。已经是星期三了,但祝你有个愉快的一周:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多