iphone开发(一)

来源:互联网 发布:小学 考试软件 编辑:程序博客网 时间:2024/05/21 08:41

      做苹果开发有一个多月了,还是不习惯MaC系统,人家都说MAC可以提供码农的开发效率,还是没感觉出来。看来大部分AIr笔记本上 装的是window7 系统。。。。。

我的开发系统:10.7 +xcode 4.3(注:xcode4.2 之前 区别很大),xcode 4.3 的资料也相对较少,对想入门的菜鸟们,头痛的!同时IOS5  引入了 Storyboard ,给开发多了一个选择,有时候多一个选择未必是好事,尤其对全局不是很清楚的条件下。接下来我 一三种方式来实现一个带有搜索的视图页面,如图1 所示

                 图1

方法一 基于strongboard 形式 

打开 xcode--->file---->new project ---->application ---->single project, 具体步骤如1-1 1-2   1-3 所示


                                                                      图1-1


                                                                       图1-2


图1-3

从工具箱中拖出,search bar 和table view 如图1 布局,这时候run下,可以看到图1的界面已有,接下来是实现它的搜索功能:

 把Editor 视图 切换至 show the  assistant Editor ,view视图 换到utilities。选择刚才的SearchBar ,右键,在reference outlet  拖出一直线到,Bidviewcontroller 建立Searchbar 与视图控制器关联(如图1-4所示);对table View 进行同样操作。


  你会发现,ide 会在我们BidViewControler.h 里增加了

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *table

在BidViewControler.m 增加了

@synthesize searchBar;
@synthesize table;

由于我们要对seachbar 和tableview的操作,所以必须让BidViewControler 实现

 <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>三个协议,看三个协议的文档,UITableViewDataSource要求必须实现
 – tableView:numberOfRowsInSection:  required method
– tableView:cellForRowAtIndexPath:  required method 
实现这两个。我们实现了
 #pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if(allTableData == nil)
        return 0;
    else {
        
        return [self.allTableData count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUInteger section  = [indexPath section];
    NSUInteger row = [indexPath row];
    
    static NSString *sectionsTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionsTableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionsTableIdentifier] ;
    }
    cell.textLabel.text = [allTableData  objectAtIndex:row];
    return cell;
    
}

#pragma mark -Table view Delegate Methods
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [searchBar resignFirstResponder];
    return indexPath;
}

#pragma mark- Search Bar Delegate Methods
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    allTableData = [[NSMutableArray alloc] initWithObjects:
                    @"Steak",@"Rare", 
                    @"Steak" ,@"Medium",
                    nil ];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"first" message:@"first by segment" delegate:self cancelButtonTitle:@"are you sure" otherButtonTitles: nil];
    [alert show];

再次运行发现,点击search 还是没反应,为啥子尼?苹果为了解耦 ,大部分都以代理模式实现,点击dock  中的searchbar  ,切换到connection inspector 你会发现,searchbar  deleagte 处于无连接转状态,赶紧拉一条直线到view controler 那里,如图1-5所示


在次运行代码,并点search 会发现,会跳出一个alert 对话框,说明我么已经搞定searchbar了,接下来的任务是实现,对tableview 的操作

我们在-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar方法中 增加[table reloaddata],运行代码后 发现 talbeview 还会没数据,为什么?? 对头。。。跟searchbar 一样,同样也要设置delegate 和datasource, 把他们连接viewcontrole(如图1-6所示)r,再次运行,哈哈,可以了。。。 


法(二),大家可以参考beginning IOs5 development 8.5 节

法(三)待续

原创粉丝点击