【问题标题】:Great UIKit/Objective-C code snippets很棒的 UIKit/Objective-C 代码片段
【发布时间】:2011-03-02 09:30:17
【问题描述】:

Objective-C iPhone/iPod touch/iPad 开发新手,但我开始发现单行代码的强大功能,例如:

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;

这将在您的应用程序 iphone 上显示带有数字 10 的独特红色通知徽章。

请在此处分享您最喜欢的用于 iPhone/iPod touch/iPad 的 Objective-C 中的一两行代码。 仅限公共 API

【问题讨论】:

    标签: iphone objective-c iphone-sdk-3.0 uikit


    【解决方案1】:

    在 Safari 中打开网址

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/"]];
    

    隐藏状态栏

    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
    

    拨打电话号码(仅限 iPhone)

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9662256888"]];
    

    启动 Apple 邮件

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://mymail@myserver.com"]];
    

    停止响应触摸事件

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    

    激活触摸事件

    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    

    显示网络活动指示器

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    

    隐藏网络活动指示器

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    

    防止 iPhone 进入睡眠模式

    [UIApplication sharedApplication].idleTimerDisabled = YES;
    

    【讨论】:

      【解决方案2】:
      1. 显示警告窗口:

        UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [alert show] 
        
      2. 获取 Documents 文件夹的路径:

        NSArray*  paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString* documentsDirectory = [paths objectAtIndex:0];
        
      3. 将另一个视图控制器推到导航栏上:

        [self.navigationController pushViewController:anotherVC animated:YES];
        
      4. 通过将 alpha 设置为 0 来淡出 UIView:

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];  // fade away over 1 seconds
        [aView setAlpha:0]; 
        [UIView commitAnimations];                      
        
      5. 获取应用的名称

        self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
        
      6. 将状态栏改为黑色

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
        
      7. 更改导航栏的样式(在视图控制器中):

        self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
        
      8. 将 NSString 保存到 NSUserDefaults:

        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:loginName forKey:kUserLoginName];
        
      9. 从 NSUserDefaults 获取一个 NSString:

        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        

        NSString* loginName = [默认 stringForKey:kUserLoginName];

      10. 在调用方法之前检查以确保对象支持方法:

        if ([item respondsToSelector:@selector(activateBOP:)]) {
            [item activateBOP:closeBOP];
        }
        
      11. 记录类名和函数名:

        NSLog(@"%s", __PRETTY_FUNCTION__);
        
      12. 在任何 UIView 项目(自身)周围添加圆角和/或边框

        self.layer.borderColor  = [UIColor whiteColor].
        self.layer.cornerRadius = 8;     // rounded corners
        self.layer.masksToBounds = YES;  // prevent drawing outside border
        
      13. 打开谷歌地图应用程序,显示两个纬度/经度点之间的方向

        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirflg=d", start.latitude, start.longitude, finish.latitude, finish.longitude];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; 
        

      【讨论】:

      • -[UIAlertView show] 返回void。因此,您正在泄漏内存。
      • 哎呀,我试图缩短它并搞砸了。我会解决的,谢谢!
      • 没问题!有时,我希望所有方法都返回self;然后,我回想起旧的 NeXTSTEP 代码并记住为什么不... :)
      • 那些日子,几乎整个程序都可以放在一行中:)
      【解决方案3】:

      将布尔值保存到用户默认值

      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Yes Bool"];
      

      将文件从 x 复制到 y

      [[NSFileManager defaultManager] copyItemAtPath:x toPath:y error:nil];
      

      显示新视图

      [self presentModalViewController:(UIViewController *) animated:YES];
      

      屏幕触摸方法

      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
      

      获取文档目录

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      

      加载网址

      [MyWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://couleeapps.hostei.com"]]];  
      

      获取当前日期和时间:

      NSCalendar *gregorian = [NSCalendar currentCalendar];
      NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
      

      自己的枚举类型:

      typedef enum {
          a = 0, b = 1, c = 2
      } enumName;
      

      石英画弧

      CGContextRef ctxt = UIGraphicsGetCurrentContext();
      CGContextAddArc(ctxt, x, y, radius, startDeg, endDeg);
      

      【讨论】:

      • 它是 -setBool:forKey: 不是 -setBool:ForKey: 所以你可能想解决这个问题。
      【解决方案4】:

      让设备振动:

      AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
      

      使用特定电话号码打开消息应用:

      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:123456789"]];
      

      停止响应触摸事件:

      [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
      

      重新开始回复:

      [[UIApplication sharedApplication] endIgnoringInteractionEvents];
      

      最后,the single line of code browser

      [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [urlText stringValue]]]];
      

      【讨论】:

        【解决方案5】:

        更改 UINavigationView 上后退按钮的标题。在推送视图之前在 UINavigationController 上使用此代码

        UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];
        
        
        self.navigationItem.backBarButtonItem = backBarButtonItem;
        [backBarButtonItem release];
        

        【讨论】:

          猜你喜欢
          • 2011-07-13
          • 1970-01-01
          • 1970-01-01
          • 2010-09-13
          • 1970-01-01
          • 1970-01-01
          • 2011-07-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多