搜尋此網誌

顯示具有 objective-c 標籤的文章。 顯示所有文章
顯示具有 objective-c 標籤的文章。 顯示所有文章

2012年10月24日 星期三

如何禁止切換TabBar(enable)


//如果不是自己的viewControll
UIViewController *controller =[self.storyboard instantiateViewControllerWithIdentifier: @"tabbarView" ];
[controller.tabBarController setDelegate:self];

//禁止切換disable
self.tabBarController.tabBar.userInteractionEnabled=NO;

2012年10月21日 星期日

iOS的影片播放 MediaPlayer vs AVPlayer


在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因為很方便使用,所以就一直使用下去。但是隨著客戶的要求越來越嚴苛,尤其是過場動畫或互動效果上的表現。所以如果在一些動畫中還挾帶影片一起運算,那勢必機器會跑不動。所以在iOS 4之後,我們可以使用AVPlayer這個類別來進行更細微的操作。
備註:
  • MediaPlayer的影片是放在UIView 裡面,而AVPlayer是放在AVPlayerLayer裡面,AVPlayerLayer是CALayer 的子類別。
  • 使用MediaPlayer前,要記得加入MediaPlayer.framework及#import <MediaPlayer/MediaPlayer.h>
  • 使用AVPlayer前,要記得加入AVFoundation.frameworkk及#import <AVFoundation/AVFoundation.h>
請參考以下的範例:

2012年10月10日 星期三

關於html資料 decode ,unescape

由於我需要取得JSON吐出來的HTML呈現在UIWebView但是發現在取得JSON不可以直接用HTML吐出來的資料


所以API資料需要先escape 將HTML tag符號換掉接著我需要在objective-c這邊將資料在unescape


做法如下先取得GTMNSString+HTML.hGTMNSString+HTML.m可由下列取得http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/https://github.com/mwaterfall/MWFeedParser/tree/master/Classes

使用方式:
#import "GTMNSString+HTML.h"


NSString *str=@"aaaaa";
[str gtm_stringByUnescapingFromHTML]



2012年10月8日 星期一

認識 UIWebView


  1. //設定網址字串  
  2. NSString *urlAddress = @"http://ithelp.ithome.com.tw/question/10056941";  
  3.   
  4. //建立一個NSURL物件  
  5. NSURL *url = [NSURL URLWithString:urlAddress];  
  6.   
  7. //建立一個NSURLRequest物件  
  8. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];  
  9.   
  10. //建立一個UIWebView 物件  
  11. UIWebView *webView = [[UIWebView alloc] initWithFrame:[self.view frame]];  
  12.   
  13. //讓 UIWebView 連上NSURLRequest 物件所設定好的網址  
  14. [webView loadRequest:requestObj];  
  15.   
  16. //將 UIWebVeiw 物件加入到現有的 View 上  
  17. [self.view addSubview:webView];  
  18.   
  19. //釋放 UIWebView佔用的記憶體  
  20. [webView release];  

2012年9月27日 星期四

更新到ios6 畫面旋轉的問題解決


其實這篇很單純
如果要符合iOS 6
不需要改動程式其他部分只要加上

  1. -(NSUInteger)supportedInterfaceOrientations{  
  2.     return UIInterfaceOrientationMaskLandscape;  
  3. }  
  4.   
  5. - (BOOL)shouldAutorotate  
  6. {  
  7.     return YES;  
  8. }  

要怎麼轉動請改第一個supportedInterfaceOrientations

原文如下:
iOS 6的rotation改变了很多。先来看看官方的描述  http://www.bgr.com/2012/08/06/ios-6-beta-4-change-log-now-available/

2012年9月25日 星期二

How to Draw Pixel-Perfect iPhone Toolbar Icons

http://steveweller.com/articles/toolbar-icons/

[Note: I have created a companion article that covers tab bar icons. It includes an updated template with new guides and a new example project.]
Creating good-looking iPhone toolbar icons is definitely a challenge. The background must be transparent and the icon antialiased. It's also white, so you'd like to work in white against a dark background and have some sort of preview that you can reference. Unless you're a Photoshop or Illustrator jockey, it's hard to know where to start.

I create pixel-perfect icons for my iPhone apps using OmniGraffle, a toolbar icon template, and a simple workflow. You can buy OmniGraffle from Omni Group and get the rest here. Note that although I'm creating a bit-mapped PNG as the final output, I'm working with vector images. This gives me the freedom to adjust and tweak as needed for the best output.

Download the template. It contains an OmniGraffle 5.1 document and an example Xcode project that uses the UIToolbarItem icon.

關於UITabbar 圖片



  • 屬於 Bar  的有
    - Navigation bars
    - Toolbars
    - Tabbars
  • Style/Image 可透過
    - (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag;
    - (id)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;
  • Style
    typedef enum {
        UITabBarSystemItemMore,
        UITabBarSystemItemFavorites,
        UITabBarSystemItemFeatured,
        UITabBarSystemItemTopRated,
        UITabBarSystemItemRecents,
        UITabBarSystemItemContacts,
        UITabBarSystemItemHistory,
        UITabBarSystemItemBookmarks,
        UITabBarSystemItemSearch,
        UITabBarSystemItemDownloads,
        UITabBarSystemItemMostRecent,
        UITabBarSystemItemMostViewed,
    } UITabBarSystemItem;
  • TabBar icon 大小
    32 x 32 png 背景為透明。
  • TabBar icon 作圖,參考
    How to Draw Pixel-Perfect iPhone Toolbar Icons
    Generating icons for iPhone UITabBar
    tabbaritem的png無法顯示
  • 顯示圖片設定,實際顯示的圖片會便
    UIImage* aImage = [UIImage imageNamed:@"favorites.png"];
    [self.tabBarItem setImage:aImage];
  • 內建直接設定 badge,後面帶入 NSString
    [self.tabBarItem setBadgeValue:@"100"];
  • 承上面兩項,實際顯示的圖形會被系統再處理一次
    原本被選取時沒被選取時
    image
    32 x 32
    imageimage
  • 2012年9月23日 星期日

    evnet click view 點擊畫面的事件觸發


    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self performSegueWithIdentifier:@"LoginSegue" sender:nil];
    }


    You can use UIView subclass and handle touch event there. Methods to look at (defined inUIResponder):
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    If you want to use "invisible" UIButton you should set its type to UIButtonTypeCustom - by default it will appear with no image and title and with transparent background. So technically it will be invisible to user but still be able to respond to all events.

    2012年9月17日 星期一

    [轉載] Objective C 字串連接 & 型別轉換




    /*字串連接*/
    在java裡面我們常常用到"+"來連接兩個字串 但在objective c似乎不是這樣用
    以下用一個例子解釋
    float weight=benefit2/7700;
    //將float轉成string
    NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
    NSString* Text1=@"你會瘦";
    NSString* Text2=@"kg";
    //將三個字串連結在一起
    NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];

    2012年9月13日 星期四

    searchBar keybroad

    當點到keybroad外面時要怎麼將鍵盤消除

    方法一

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
        [searchBar resignFirstResponder];
        // Do the search...
    }


    方法二

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       [self.view endEditing:YES];
    }
    

    2012年9月12日 星期三

    AFNetworking使用範例


    直接使用


    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
          
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response,NSError *error, id JSON) {
            NSLog(@"error:%@,response:%@",error,response);
            
        }];
    [operation start];


    2012年9月9日 星期日

    objective-c blocks

    轉貼:http://blog.sina.com.cn/s/blog_67419c420100vl8b.html

     在ios4之后,引入了代码块的特性,在gcd中会经常的用到,所以决定好好的看看代码块文档,把这块总结一下。从头开始讲解代码块。

    2012年9月7日 星期五

    Blocks


    轉貼:http://lldong.github.com/blog/2011/12/30/blocks/
    从Mac OS X 10.6以及iOS 4开始,苹果在GCC和Clang编译器中为C语言引入了一个新扩展:Blocks,使得程序员可以在C、Objective-C、C++和Objective-C中使用闭包。Blocks有点像函数,但是它可以在其它函数或方法中进行声明和定义,同时它还是匿名的(匿名函数),并可以捕获其所在作用域中的变量(闭包特性)。

    详解Objective-c Block应用


    目前只有Mac 10.6 和iOS 4有支援。Block是由一堆可執行的程式組成,也可以稱做沒有名字的Function (Anonymous function)
    详解Objective-c block应用是本文要介绍的内容,主要介绍的是Objective-c block应用,不多说,先来看详细内容。
    Apple在C, Objective-C, C++加上Block這個延申用法。目前只有Mac 10.6 和iOS 4有支援。Block是由一堆可執行的程式組成,也可以稱做沒有名字的Function (Anonymous function)。如果是Mac 10.6 或 iOS 4.0 之前的平台可以利用 http://code.google.com/p/plblocks/ 這個project得以支援Block語法。
    Apple有一個叫做GCD(Grand Central Dispach)的新功能,用在同步處理(concurrency)的環境下有更好的效率。Block語法產生的動機就是來自於GCD,用Block包好一個工作量交給GCD,GCD有一個宏觀的視野可以來分配CPU,GPU,Memory的來下最好的決定。
    Block 簡介
    Block其實行為和Function很像,最大的差別是在可以存取同一個Scope的變數值。
    Block 實體會長成這樣
    ^(傳入參數列) {行為主體};
    Block實體開頭是"^",接著是由小括號所包起來的參數列(比如 int a, int b, float c),行為的主體由大括號包起來,專有名詞叫做block literal。行為主體可以用return回傳值,型別會被compiler自動辦識出來。如果沒有參數列要這樣寫(void)。
    看個列子
    1. ^(int a) {return a*a;}; 

    Objective-C block


    Block memory

    block 的内存管理,应该是最头疼的地方,就用这个来自WWDC的例子来解释一下吧。
    image
    当程序运行到这里时,stack 空间中有 shared 变量和 captured 变量。
    这里可以看出,__block 变量开始是处于stack上的。

    2012年9月6日 星期四

    NSTimer


    @interface
        NSTimer *autoTimer;
    @implementation
    // Start timer
        autoTimer = [NSTimer scheduledTimerWithTimeInterval:(3.0)
            target:self 
            selector:@selector(autoTimerFired:) 
            userInfo:nil 
            repeats:YES];
    // Stop timer:
        [autoTimer invalidate];
        autoTimer = nil;

    iOS开发 对话框UIActionSheet和UIAlertView用法


    UIActionSheet和UIAlertView在iOS中,都是弹出一个对话框,用户必须点击上面的按钮后才能执行其他操作。
    下图是UIActionSheet,对话框显示在底部:

    iphone屏幕旋转、改变 视图尺寸 和 布局控制


    如果  你  曾经  用  用户界面套件  开发过  桌面窗口应用程序(比如:Windows,Mac OS X,Linux,Qt  或者  GTK+),你  就应该了解到  在窗口的尺寸改变时  我们设计的用户界面  也要能  正确地  改变  自己的尺寸。在这个过程中  我们  要确定  窗口中  哪些肉眼可见的部件  要移动位置,哪些  要根据窗口的变化  改变  自己的尺寸。
    有  一件事情  很明显:在iphone  和  ipad这种运行iOS的设备上  窗口  并不能  像桌面应用程序一样  允许  用户  改变。然而  这样的设备  可以感知  物理上的转动,从而  允许  应用程序  以竖屏模式  或者  横屏模式  运行。既然  iOS设备  有  这样的能力,那么  我们  很有  必要  使  我们的应用程序用户界面布局  根据  设备的旋转  而  改变。
    接下来  我们  要了解  设备转动  和  用户界面重新布局的机制。

    判斷是哪個class


    // DO NOT DO THIS!
    if ([myArray isKindOfClass:[NSMutableArray class]])
    {
        // Modify the object
    }

    2012年9月4日 星期二

    如何判斷是哪個alert


    方法一
    @property (nonatomic, strong) UIAlertView *passwordAlert;
    @property (nonatomic, strong) UIAlertView *warningAlert;
    
    passwordAlert = [[UIAlertView alloc] initWithTitle:@"Password" message:@"Enter password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
    passwordAlert.alertViewStyle = UIAlertViewStyleSecureTextInput;
    [passwordAlert show];
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
       if (alertView == passwordAlert) {
          // handle anything for password button indexes here
       }
       if (alertView == warningAlert)_ {
          // handle anything for warning button indexes here
       }
    }
    方法二