IB动态表VS.编程的行动表

时间:2011-02-25 16:24:50

标签: iphone ios cocoa-touch uiactionsheet

问候所有人。

我正在尝试实施Nate Weinders的“ShareKit”框架。一切顺利,直到最后一步。

我在Interface Builder中设置了一个名为“shareBtn”的通用按钮设置

在我的.h我有:

-(IBAction)shareBtn;

在我的.m中,我设置了一个通用的(工作)UIActionSheet:

最重要的是我拉入ShareKit框架:#import“SHK.h”

对于我的按钮,我有这个:

 -(IBAction)shareBtn {
         UIActionSheet *actionsheet = [[UIActionSheet alloc] 
                                       initWithTitle:@"Which do you prefer?"
                                       delegate:self 
                                       cancelButtonTitle:@"Cancel" 
                                       destructiveButtonTitle:@"Destuctive Button" 
                                       otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil
                                       ];
         [actionsheet showInView:[self view]];
         [actionsheet release];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"button %i clicked", buttonIndex );
}

这一切都很好,花花公子。现在,根据ShareKit安装页面上的说明,我将放置此代码以使动作表挂钩到sharekit框架中:

- (void)myButtonHandlerAction
{
    // Create the item to share (in this example, a url)
    NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
    SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];

    // Get the ShareKit action sheet
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    // Display the action sheet
    [actionSheet showFromToolbar:navigationController.toolbar];
}

看起来相当直接,我玩了一些语法(使用self.toolbar等等),我只是在努力去理解这个概念和我所缺少的东西。我读了关于这个的iOS文档,我引用了我的几本书,并做了很多在线搜索。

希望可以看到明显的错误或帮助指导我。

谢谢。

以下是我尝试的内容和错误:

首次尝试

 -(IBAction)shareBtn {
    // Create the item to share (in this example, a url)
    NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
    SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];

    // Get the ShareKit action sheet
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    // Display the action sheet
    [actionSheet showFromToolbar:navigationController.toolbar];
}

错误:'navigationController undeclared'

///////第二次尝试///////

 -(IBAction)shareBtn {
    // Create the item to share (in this example, a url)
    NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
    SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];

    // Get the ShareKit action sheet
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    // Display the action sheet
    [actionSheet self.toolbar];
}

//////错误:'预期']'之前'。'令牌'//////////

///////第三次尝试///////

 -(IBAction)shareBtn {
    // Create the item to share (in this example, a url)
    NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
    SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];

    // Get the ShareKit action sheet
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    // Display the action sheet
    [actionSheet self.toolbar];
}

//////错误:'预期']'之前'。'令牌'//////////

///////第四次尝试///////

-(IBAction)shareBtn {
    UIActionSheet *actionsheet = [[UIActionSheet alloc] init];

    // Create the item to share (in this example, a url)
    NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
    SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];

    // Get the ShareKit action sheet
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    // Display the action sheet
    [actionsheet showInView:[self view]];
    [actionsheet release];
}

//////错误:未使用的变量'actionSheet'////////// //////应用程序启动,但就在操作表弹出时,它会崩溃///////

仅供参考:ShareKit安装页面:[url = http://www.getsharekit.com/install/] ShareKit:安装[/ url]

ALSO: 我现在正在阅读“Big Nerd Ranch Guides - iPhone Programming”。

作为一种学习方式,我尝试应用我一直在学习的一些概念(其中一些概念超出了我目前的知识)。我一般都了解具体的错误,但我却迷失了如何实施这一行动。

让我对这个actionSheet感到困惑的是,它与我学到的第一种方法完全吻合。我现在正试图通过拉入ShareKit框架并使其执行来扩展这些知识。这看起来很直接,我真的很沮丧......

这是ShareKit安装(它说“在三行简单的代码中”) 第4步: http://www.getsharekit.com/install/

1 个答案:

答案 0 :(得分:4)

我开始工作了!

最终守则:

-(IBAction)shareBtn {
    // Create the item to share (in this example, a url)
    NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
    SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];

    // Get the ShareKit action sheet
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];

    // Display the action sheet
    [actionSheet showInView:[self view]];
    [actionSheet release];
}

让我感到困惑的是两个愚蠢的东西,一个我没有在showInView或发行版的actionSheet中大写“S”。第二个是使用showInView:[self view]];而不是.toolbar方法。

谢谢,我知道只需要一点提示就可以帮我调试。