iOS笔记

本文详细介绍了iOS开发中File's Owner的概念,它相当于xib文件对应的类,如view controller。File's Owner用于连接xib中的控件和类中的属性,实现事件响应。此外,文章还探讨了view与viewController的关系,状态栏样式设置,以及使用xib文件和Storyboard进行界面设计的方法。文中涉及的知识点还包括事件处理、视图控制器生命周期、滚动视图和PageControl的使用,以及第三方库的介绍和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

July 27, 2016
作者:dengshuai_super
出处:http://blog.csdn.net/dengshuai_super/article/details/52050398
声明:转载请注明作者及出处。


✓   2015.6.4 周四

file’s owner以及outlet与连线的理解

file’s owner 可以看作是xib对应的类,比如view对应的xib文件的file‘s owner
对应的就是view controller的类。

我对file’s owner 的理解:xib文件附属于一个类,这个类就是xib属性栏中的custom Class,并且custom Class一般是控制器类A。而file’s owner相当于这个控制器类A。现在需要对控制器A中定义的类 和 xib中的形象化的控制器类B进行连接,只要从file’s owner 拖到对应的控制器B进行连接。这个过程相当对把A中的某个属性(一般都是定义的类IBOutlet)和B中的控件或者视图或者控制器对应起来。
而事件的连接的过程则相当于将B中的某个执行动作和A中的某个方法(IBAction)

view和viewController之间的对应关系,需要一个桥梁来进行连接(即,对于一个视图,他如何知道自己的界面的操作应该由谁来响应),这个桥梁就是File’s
Owner。

选中某个XIB的File’s Owner ,在Inspector中可以看到属性:File Name和Custom Class,该File’s Owner 就是用来绑定File Name中的xib文件和Custom Class中的ViewController的,在做了这个绑定之后,按住control键,拖动File‘s Owner 到xib中的某个控件的时候,就是Custom Class中定义的IBOutlet元素与xib中元素中进行连接的过程,同样,拖动“xib中的控件的动作”到File’s Owner的时候,就是将xib中该动作的响应与Custom Class中某个IBAction进行连接的过程。

xib文件本身可以看作是一个xml,app启动的时候会根据xml构造的xib对应的界面及其控件。

✓   2015.6.19周五

http://blog.csdn.net/q199109106q/article/details/8563438/

✓   2015.6.22周一

http://mobile.51cto.com/iphone-388248.htm

4、打开ViewController.m,找到addButton函数,添加以下代码:

    1.  - (IBAction)addButton:(id)sender { 
    2.      //动态添加一个按钮 
    3.      CGRect frame = CGRectMake(300, 300, 300, 50);  
    4.      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    5.      button.frame = frame; 
    6.      [button setTitle:@"新添加的动态按钮" forState: UIControlStateNormal];   
    7.      button.backgroundColor = [UIColor clearColor];   
    8.      button.tag = 2000; 
    9.      [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];   
    10.     [self.view addSubview:button];   
    11. } 

5、在addButton函数后边添加一个函数:

    1.  //这个是新按钮的响应函数 
    2.  -(IBAction) buttonClicked:(id)sender {   
    3.      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  
    4.                                                      message:@"单击了动态按钮!"    
    5.                                                     delegate:self    
    6.                                            cancelButtonTitle:@"确定"   
    7.                                            otherButtonTitles:nil];   
    8.      [alert show]; 
    9.      [alert release]; 
    10. } 
    11. 

在深色背景下将状态条设置成白色:

在老版本的iOS中,状态栏永远都是白色风格。而在iOS 7中,我们可以修改每个view controller中状态栏的外观。通过UIStatusBarStyle常量可以指定状态栏的内容是暗色或亮色。默认情况下,状态栏的显示是暗色。也就是说,状态栏上的时间、电池指示器和Wi-Fi信号显示为暗色。如果导航栏中使用暗色为背景

-(UIStatusBarStyle)preferredStatusBarStyle 
{ 
    return UIStatusBarStyleLightContent; 
}

通过tag值获得相应对象

UITabBarItem *shouye = (UITabBarItem *)[self.view viewWithTag:100];

改变TabBarItem的选中时和未选中时的图片
UITabBarItem *ti1 = [tabBar.items objectAtIndex:0];
ti1.title = @”梦见”;

ti1.image = [[UIImage imageNamed:@"qr_tabbar_mine"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
ti1.selectedImage = [[UIImage imageNamed:@"qr_tabbar_mine_hl"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 

http://www.bkjia.com/IOSjc/872299.html
3.以上拖拽工作到此结束,下面要实现我们的业务逻辑和关联视图之间的关系,为了关联视图时能找到带有三个按钮的视图,我们需要设置一下该视图的StoryboardID,入下图

  4.下面来编写我们的代码,上面我们用到了TextField,我们需要处理键盘的回收事件,所以我们的ViewController要遵守UITextFiledDelegate协议,实现有关键盘的方法
    (1)遵守UITextFieldDelegate协议

#import <UIKit/UIKit.h>   
@interface ViewController : UIViewController<UITextFieldDelegate> 
@end

    (2)在ViewController.m中中进行回调注册和实现协议中相应的方法,代码如下:

-(BOOL) textFieldShouldReturn:(UITextField *)textField {     
[self.userName resignFirstResponder];     [self.password resignFirstResponder];    
return YES; 
}   
- (void)viewDidLoad {     
[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    self.userName.delegate = self;     self.password.delegate = self; 
- }

  5.处理完键盘的事儿,就该处理我们当登陆按钮点击时回调的事件了,首先在回调方法中获取TextFiled的值,由值的情况来实现是否进行页面间的切换。 在页面切换时我们得关联两个页面中的关系。

- (IBAction)tapButton:(id)sender {           
if ([username isEqualToString:@"admin"] && [password isEqualToString:@"admin"])     
{
//获取storyboard: 通过bundle根据storyboard的名字来获取我们的storyboard,         
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];    //由storyboard根据myView的storyBoardID来获取我们要切换的视图        
UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"myView"];//由navigationController推向我们要推向的view         
[self.navigationController pushViewController:myView animated:YES];               
}   
}

 代码说明:关联两个View需要三部
1.获取storyboard: 通过bundle的名获取bundle, 在通过storyborad的名字来获取我们的storyboard;
2.在由storyboard获取storyboardID是myView的View;
3.执行由当前View推向我们获取到的myView;

使用storyboard,设置tab bar Item的选中图片(selected Image)
http://blog.csdn.net/sxh1234567/article/details/44984665

                                       9:16


这里写图片描述

http://www.cocoachina.com/ios/20141230/10800.html
快速兼容iPhone5s和6,6plus

✓   2015.6.23 周二
static NSString *CellIdentifier= @"CellIdentifier";

#define PLISTFILENAME @"SpeMerchantPList.plist"

#define PATH  [[NSBundle mainBundle]pathForResource:PLISTFILENAME ofType:nil]

- (void)viewDidLoad {
    [super viewDidLoad];
   // [_SpecialMerchantTableView setBackgroundColor:[UIColor grayColor]];//把背景色改为黑色背景
    _SpeMerchantInfoArray = [NSArray arrayWithContentsOfFile:PATH];

    [self.tableView registerClass:[UITableViewCell class]  forCellReuseIdentifier:CellIdentifier];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [_SpeMerchantInfoArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    // Configure the cell...
    if ([_SpeMerchantInfoArray count]>0)
    {

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(7, 7, 100, 70)];
        imageView.image = [UIImage imageNamed:_SpeMerchantInfoArray[indexPath.row][0]];
        [cell addSubview:imageView];

        UILabel *TitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(115, 8, 95, 21)];
        TitleLabel.text = _SpeMerchantInfoArray[indexPath.row][1];
        [cell addSubview:TitleLabel];

        UILabel *DateLabel = [[UILabel alloc]initWithFrame:CGRectMake(121, 39, 160, 17)];
        DateLabel.text = _SpeMerchantInfoArray[indexPath.row][2];
        DateLabel.backgroundColor = [UIColor clearColor];
        DateLabel.textColor = [UIColor grayColor];
        DateLabel.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:5];
        [cell addSubview:DateLabel];

        UILabel *JuLiLabel = [[UILabel alloc]initWithFrame:CGRectMake(240, 7, 63, 21)];
        JuLiLabel.text = _SpeMerchantInfoArray[indexPath.row][3];
        [cell addSubview:JuLiLabel];

        UIButton *zuobiaoButton = [[UIButton alloc]initWithFrame:CGRectMake(215, 7, 20, 20)];
       // [zuobiaoButton.imageView setImage:[UIImage imageNamed:@"zaixiandaohang"] ];
        [zuobiaoButton setImage:[UIImage imageNamed:@"zaixiandaohang"] forState:UIControlStateNormal];
        [cell addSubview:zuobiaoButton];

        UIButton *moneyButton = [[UIButton alloc]initWithFrame:CGRectMake(303, 7, 20, 20)];
        //[moneyButton.imageView setImage:[UIImage imageNamed:@"caifu"] ];
        [moneyButton setImage:[UIImage imageNamed:@"caifu"] forState:UIControlStateNormal ];
        [cell addSubview:moneyButton];


    }


    return cell;
}
✓   2015.6.24 周三

http://www.cocoachina.com/bbs/read.php?tid-281373.html

//自定义按钮,文字在左,图片在右
HHButton *btn = [[HHButton alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];//宽100,高30只是最大的显示面积。 
[btn setTitle:@"山东省青岛市" forState:UIControlStateNormal]; 
[btn setImage:[UIImage imageNamed:@"小三角"  forState:UIControlStateNormal]; 


 [[UITabBar appearance] setSelectedImageTintColor:[UIColor orangeColor]];

init-初始化程序
viewDidLoad-加载视图
viewWillAppear-UIViewController对象的视图即将加入窗口时调用;
viewDidApper-UIViewController对象的视图已经加入到窗口时调用;viewWillDisappear-UIViewController对象的视图即将消失、被覆盖或是隐藏时调用;viewDidDisappear-UIViewController对象的视图已经消失、被覆盖或是隐藏时调用;didReceiveMemoryWarning - 内存不足时候调用这个

当利用ScrollView实现滚动页面时,初始化ScrollView时CGRectMake当width要设置成ScreenWidth,而它的ContentSize的宽度设置成实际内容所需要的宽度,例如有3张图片则设置成3倍图片的宽度。

http://www.cnblogs.com/woainilsr/archive/2012/03/28/2421881.html

length = (self.view.frame.size.width-40)/6;
    Scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, smallScroll.frame.origin.y+smallScroll.frame.size.height, screenWidth, length*1.8)];
    Scroll.bounces = NO;
    Scroll.backgroundColor=[UIColor whiteColor];
    Scroll.showsHorizontalScrollIndicator = NO;
    Scroll.pagingEnabled = YES;
    Scroll.delegate = self;
    Scroll.contentSize = CGSizeMake(self.view.frame.size.width*2, length*1.8);
    [scrollView addSubview:Scroll];

Scroll.contentSize = CGSizeMake(self.view.frame.size.width*buttonTitleArr.count/4, length*1.8);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值