IOS開發基礎知識--片段12,ios基礎知識--片段

來源:互聯網
上載者:User

IOS開發基礎知識--片段12,ios基礎知識--片段

1:Delegate運用

.h#import <UIKit/UIKit.h>@protocol FilterHeaderViewDelegate <NSObject>@required-(void)filterHeaderViewMoreBtnClicked:(id)sender;@endextern float CYLFilterHeaderViewHeigt;@interface FilterHeaderView : UICollectionReusableView@property (nonatomic, weak  ) id<FilterHeaderViewDelegate> delegate;@end.m- (void)moreBtnClicked:(id)sender {    if ([self.delegate respondsToSelector:@selector(filterHeaderViewMoreBtnClicked:)]) {        [self.delegate filterHeaderViewMoreBtnClicked:self.moreButton];    }}注意在.m中會有要調用上面這個方法:[self.moreButton addTarget:self action:@selector(moreBtnClicked:) forControlEvents:UIControlEventTouchUpInside];而在調用這個外掛程式的時候記得把delegate賦於self,並要把FilterHeaderViewDelegate引入到<>裡;然後就可以實現這個deletage的方法;

2:實現UIButton不同狀態下的顯示

[btn setTitle:@"更多" forState:UIControlStateNormal];    [btn setTitle:@"收合" forState:UIControlStateSelected];    btn.titleLabel.font = [UIFont systemFontOfSize:12];    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];    btn.adjustsImageWhenHighlighted = NO;    [btn setImage:[UIImage imageNamed:@"home_btn_more_normal"] forState:UIControlStateNormal];    [btn setImage:[UIImage imageNamed:@"home_btn_more_selected"] forState:UIControlStateSelected];    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width-kImageToTextMargin, 0, btn.imageView.frame.size.width);    btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width, 0, -btn.titleLabel.frame.size.width);

3:視圖uiview增加點擊事件

    if (!_TicketView) {        _TicketView=[UIView new];        _TicketView.backgroundColor=[UIColor whiteColor];        //增加點擊事件        UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TickAction:)];        [_TicketView addGestureRecognizer:tapGesture];        [_BelowView addSubview:_TicketView];    }調用:-(void)TickAction:(id)sender{    NSLog(@"sdfsdfsdf");}

4:在ios7中出現滾動視圖UIScrollView不相容問題,無法滾動

把contentSize定義放在viewDidLayoutSubviews中;-(void)viewDidLayoutSubviews{    _myScrollView.contentSize=CGSizeMake(SCREEN_WIDTH,600);}

5:對UIButton上的表徵圖進行翻轉

#define DEGREES_TO_RADIANS(angle) ((angle)/180.0 *M_PI)調用:_BtnMoreContent.imageView.transform = CGAffineTransformRotate(_BtnMoreContent.imageView.transform, DEGREES_TO_RADIANS(180));

6:建立一個背景圖片,並從網路動態載入

在viewDidLoad調用:-(void)LoadBackViewImage{    UIImageView *bgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];        bgView.image=[UIImage imageNamed:@"bgWeatherEmpty"];        [bgView sd_setImageWithURL:[NSURL URLWithString:self.backImageUrl] placeholderImage:[UIImage imageNamed:@"bgWeatherEmpty"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {        dispatch_async(dispatch_get_main_queue(), ^{            bgView.image=image;            [self.view addSubview:bgView];            [self.view sendSubviewToBack:bgView];        });    }];}注意:UIView層次管理(sendSubviewToBack,bringSubviewToFront) 將一個UIView顯示在最前面只需要調用其父視圖的 bringSubviewToFront()方法。將一個UIView層推送到背後只需要調用其父視圖的 sendSubviewToBack()方法。

7:載入xib到其它視圖中

建立一個UIView的xib檔案,這個方程可以看網上隨便一個文章,主要是把屬性中的custom Class指定給建立的那個 .h檔案;.h#import <UIKit/UIKit.h>@interface LKTextView : UIView@property (strong, nonatomic) IBOutlet UILabel *lbText;- (IBAction)bt_pressed:(id)sender;@property (strong, nonatomic) IBOutlet UITextField *textView;+(LKTextView*)instanceTextView;@end.m#import "LKTextView.h"#import "RKTabView.h"#import "RKTabItem.h"@implementation LKTextView@synthesize textView;@synthesize lbText;+(LKTextView *)instanceTextView{    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"LKTextView" owner:nil options:nil];    return [nibView objectAtIndex:0];}-(id)initWithCoder:(NSCoder *)aDecoder{    self = [super initWithCoder:aDecoder];    if(self)    {        //其它自個想增加到視圖裡的        [self initViews];            }    return self;}/** *  @author wujunyang, 15-04-30 16:04:03 * *  @brief  這邊是運用RKTABVIEW建立一個標籤的 */-(void)initViews{    UIView *vi=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];    vi.backgroundColor=[UIColor redColor];    [self addSubview:vi];        RKTabView *titledTabsView=[[RKTabView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];    [self addSubview:titledTabsView];        RKTabItem *mastercardTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"mastercard"]];    mastercardTabItem.titleString = @"MasterCard";    RKTabItem *paypalTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"paypal"]];    paypalTabItem.titleString = @"PayPal";    RKTabItem *visaTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"visa"]];    visaTabItem.titleString = @"Visa";    RKTabItem *wuTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"wu"]];    wuTabItem.titleString = @"Western Union";    RKTabItem *wireTabItem = [RKTabItem createUsualItemWithImageEnabled:nil imageDisabled:[UIImage imageNamed:@"wire-transfer"]];    wireTabItem.titleString = @"Wire Transfer";        //mastercardTabItem.tabState = TabStateEnabled;        titledTabsView.darkensBackgroundForEnabledTabs = YES;    titledTabsView.horizontalInsets = HorizontalEdgeInsetsMake(25, 25);    titledTabsView.titlesFontColor = [UIColor colorWithWhite:0.9f alpha:0.8f];        titledTabsView.tabItems = @[mastercardTabItem, paypalTabItem, visaTabItem, wuTabItem, wireTabItem];}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/- (IBAction)bt_pressed:(id)sender {    lbText.text = textView.text;}@end然後調用視圖.m:- (void)viewDidLoad{       LKTextView* text = [LKTextView instanceTextView];    text.frame = CGRectMake(100, 100, text.frame.size.width, text.frame.size.height);    text.textView.text = @"input ";   [self.view addSubview:text];    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}

 8:不錯的外掛程式

AutoLayout下自動計算UITableViewCell高度的擴充FDTemplateLayoutCell    https://github.com/forkingdog/UITableView-FDTemplateLayoutCell多視圖控制器間進行切換--XLPagerTabStrip                              https://github.com/xmartlabs/XLPagerTabStrip不錯的標籤選項卡外掛程式                                                 https://github.com/RafaelKayumov/RKTabView

9:ios7和ios8關於導覽列的那些事

ios7之前的版本中UIViewController中的view在顯示後會自動調整為去掉導覽列的高度的,控制項會自動在導覽列以下擺放。在iOS7中UIViewController的wantsFullScreenLayout屬性被捨棄了,所有的UIViewController建立後預設就是full Screen的,因此如果帶導覽列的應用介面中的部分控制項會被導覽列覆蓋掉。解決方案:可以使用ios7種UIViewController新增的屬性extendLayoutIncludesOpaqueBars和edgesForExtendedLayout來解決。其中這個屬性指定了當bar使用不透明圖片時,試圖是否延伸至bar所在地區,預設值為NO。而edgesForExtendedLayout其中這個屬性指定了當Bar使用了不透明圖片時,視圖是否延伸至Bar所在地區,預設值時NO。而edgesForExtendedLayout則是表示視圖是否覆蓋到四周的地區,預設是UIRectEdgeAll,即上下左右四個方向都會覆蓋,那麼為讓頂部不進行延伸到導覽列覆蓋的地區,我們可以把頂部地區延伸去掉。實現代碼如下:self.extendedLayoutIncludesOpaqueBars = NO;self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;

 11:initWithNibName/awakeFromNib/initWithCoder區別

第一、initWithNibName這個方法是在controller的類在IB中建立,但是通過Xcode執行個體化controller的時候用的. 第二、initWithCoder 是一個類在IB中建立但在xocde中被執行個體化時被調用的.比如,通過IB建立一個controller的nib檔案,然後在xcode中通過 initWithNibName來執行個體化這個controller,那麼這個controller的initWithCoder會被調用.或者是一個view的nib檔案,類似方法建立時調用initWithCoder 第三、awakeFromNib  當.nib檔案被載入的時候,會發送一個awakeFromNib的訊息到.nib檔案中的每個對象,每個對象都可以定義自己的awakeFromNib函數來響應這個訊息,執行一些必要的操作。也就是說通過nib檔案建立view對象時執行awakeFromNib 第四、關於 initWithNibName 和 loadNibNamed 的區別和聯絡 :   關於 initWithNibName 和 loadNibNamed 的區別和聯絡。之所以要把這兩者來一起講,我覺的我也有點困惑,到底用那種?其實真正搞清楚了他們之間的差別,就不會這麼迷惘了。因為這兩個方法,根本就不是一路貨色。  既然,是要說明這2個方法,那就著重將區別吧。  但是第一步,還是要羅嗦一下,他們的聯絡:可以使用此方法載入使用者介面(xib檔案)到我們的代碼中,這樣,可以通過操作這個載入進來的(xib)對象,來操作xib檔案內容。  下面進入主題,談區別: 1. ShowViewController的initWithNibName方法 ShowViewController * showMessage = [[ShowViewController alloc]  initWithNibName:@"ShowViewController" bundle:nil];  self.showViewController = showMessage;  [showMessage release];  2.VideoCellController的loadNibNamed方法 NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"Save3ViewController"  owner:self options:nil] ;  self.showViewController = [nib lastObject];  [nib objectAtIndex:0];  總結:  只看他們初始化,那可能感覺是一樣的。但是如果,開啟分別看xib的關係的時候,才恍然大悟,原來他們的整合類都不一樣。 1. initWithNibName要載入的xib的類為我們定義的視圖控制器類 2.載入方式不同 initWithNibName方法:是消極式載入,這個View上的控制項是 nil 的,只有到 需要顯示時,才會不是 nil  loadNibNamed方法:即時載入,用該方法載入的xib對象中的各個元素都已經存在。  (認真理解這句幫規:when using loadNibNamed:owner:options:, the File's Owner should be NSObject, the main view should be your class type, and all outlets should be hooked up to the view, not the File's Owner.)   第五、initWithCoder和initWithFrame的區別  nitWithoder 是當從nib檔案中載入對象的時候會調用,比如你的view來自nib那麼就會調用這個view的這個函數。(由架構調用) initWithFrame (是由使用者調用,來初始化對象的) 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.