【问题标题】:How can I create an interaction with a URL by clicking a button in objective c?如何通过单击目标 c 中的按钮来创建与 URL 的交互?
【发布时间】:2012-01-13 01:11:00
【问题描述】:

我正在尝试为 iphone 制作一个应用程序,它将向指定的 urf(或 Web 应用程序)发送一个 tcp 连接。我希望按钮在发送前首先发出警报。我为此写了这段代码:

-(IBAction)tiklandi:(id)sender{
NSString *buton1 = [sender titleForState:UIControlStateNormal];
NSString *uyariText = [[NSString alloc] initWithFormat:@"istek gönderiliyor!"];
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Sunucuya Gönderildi" message:uyariText delegate:nil
    cancelButtonTitle:@"işlemi iptal ettiniz!"
                                    otherButtonTitles: nil];
[alert show];
//[alert release];

但我无法在我的代码中使用任何 url 方法。

我的第二个问题是,我在 Developer 目录和 Xcode 的项目导航器中看不到我的界面构建器,我看不到文件夹,所有的类都相互混合。我使用的是 Xcode 4.2,我对所有这些都是新手,有人可以帮忙吗?


我尝试了列出的解决方案,但我不知道如何交互我的视图控制器文件和 .xib 文件。 这是我的视图控制器 .h 文件:

@interface ViewController : UIViewController


@property (nonatomic, retain) NSURLRequest *url;
@property (nonatomic,retain) UIButton *buton;


-(IBAction)fonksiyon:(id)sender;
@end

这是我的实现文件(视图控制.. .m)

#import "ViewController.h"

@implementation ViewController

@synthesize buton;
@synthesize url;



-(IBAction)fonksiyon:(id)sender{

    NSString *buton= [sender titleForState:UIControlStateNormal];
    NSURL *url= [sender url];
    NSString *uyariText= [[NSString alloc] initWithFormat:@"gönderiliyor!"];
    UIAlertView *alertView= [[UIAlertView alloc]initWithTitle:@"Sunucuya Gönderildi" 
                                                  message:uyariText
                                                 delegate:self
                                        cancelButtonTitle:@"işlemi iptal ettiniz!" 
                                        otherButtonTitles:@"Server'a Git!", nil];

    - (NSURL)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        if (buttonIndex != [alertView cancelButtonIndex]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://192.168.69.290/cgi-bin/ipad.pl";nil]];
        }

    }

}

问题是我无法通过文件所有者在 .xib 文件和其他文件之间建立关系。据我了解,当我从文件所有者那里拖动时,我也应该看到我的函数(这里称为 fonksiyon)。有人可以帮忙吗?

【问题讨论】:

    标签: iphone xcode interface-builder


    【解决方案1】:

    当您创建 UIAlertView 并实现 UIAlertViewDelegate 方法之一时,将委托设置为 self。 当然,如果你想提供取消选项,你需要一个“otherButtonTitle”

    UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Sunucuya Gönderildi" 
                                                  message:uyariText
                                                 delegate:self
                                        cancelButtonTitle:@"işlemi iptal ettiniz!" 
                                        otherButtonTitles:@"Go to website", nil];
    
    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        if (buttonIndex != [alertView cancelButtonIndex]) {
            // open url in external safari
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://foo.bar"]];
            // EDIT: I might have misunderstood your question. 
            // you should read about NSURLConnection and put that code here.
        }
    }
    

    我的第二个问题是,我在开发者目录中看不到我的界面生成器

    Interface Builder 不再是一个额外的应用程序。它现在内置在 Xcode 中。您不需要启动它,只需在 Xcode 中选择 xib 文件即可。

    而且在 Xcode 的项目导航器中我看不到文件夹,所有的类都相互混合。

    选择您要分组的文件,再次单击并选择“从选择中新建组”。 您负责对文件进行分组。当您有一个现有组时,只需将文件拖放到那里。

    【讨论】:

    • 非常感谢。我认为,由于我刚刚开始使用 xcode 和客观 c- 我无法以技术方式解释我的问题。现在唯一的问题是实现 NSURLConnection 类,我会读一点。
    【解决方案2】:
    1. 您必须实现 UIAlert 的委托协议来处理用户的响应。然后你就可以打开你想要的网址了。

    2. Interface Builder 不再是一个单独的应用程序(自 XCode v4 起)> 只需在 XC 中打开一个 XIB/NIB 文件。

    3. 要在 Safari 移动版中打开 URL,请检查以下问题:How can I launch Safari from an iPhone app?

    4. 如果你想在你的应用中打开一个 URL,你需要构建你自己的 UIWebView: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/

    【讨论】:

    • 感谢您的回复,但我无法在我创建的 .xib 文件和我的应用程序之间建立关系。而且我不想在我的应用程序中打开任何结果,它将包含一个推荐,当单击按钮时,它将将该推荐发送到服务器 - 现在我只需与 url 交互就足够了。你能再帮忙吗?
    • 为了确保您的应用了解 XIB 文件,您必须: 1. 将其分配给目标(在检查器中,在右侧)。这将自动完成。 2.确保您的XIB归您的视图控制器所有(将文件所有者设置为您的控制器类)。控制器必须从 XIB 文件加载。查看 Apple 文档中的 MVC 教程。了解更多详情。
    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多