iOS小知识点(二)

来源:互联网 发布:如何开实体淘宝店 编辑:程序博客网 时间:2024/05/01 13:40
活动表单
<UIActi*****heetDelegate>

 - (IBActive)someButtonPressed:(id)sender
{
    UIActi*****heet*acti*****heet =[[UIActi*****heetalloc] 
                initWithTitle:@”Are yousure?”
               delegate:self
                cancelButtonTitle:@”Noway!”
                destructiveButtonTitle:@”Yes,I’m Sure!”
               otherButtonTitles:nil];
   [acti*****heetshowInView:self.view];
    [acti*****heetrelease];
}

警告视图
 <UIAlertViewDelegate>

 - (void) acti*****heet:(UIActi*****heet*)acti*****heetdidDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex !=[acti*****heetcancelButtonIndex])
    {
         NSString*message = [[NSStringalloc] initWithFormat:@”You can       
               breathe easy, everything wentOK.”];
        UIAlertView *alert =[[UIAlertView alloc]   
                        initWithTitle:@”Something wasdone”
                        message:message
                         delegate:self
                        cancelButtonTitle:@”OK”
                        otherButtonTitles:nil];
         [alertshow];
        [alertrelease];
        [messagerelease];
    }
}
 
动画效果
-(void)doChange:(id)sender
{
if(view2 == nil)
{
[self loadSec];
}
[UIView beginAnimati*****:nil context:NULL];
[UIView setAnimationDuration:1];     
[UIViewsetAnimationTransition:([view1superview]?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight)forView:self.viewcache:YES];
   
    if([view1 superview]!=nil)
{
[view1 removeFromSuperview];
[self.view addSubview:view2];
 
}else {
 
[view2 removeFromSuperview];
[self.view addSubview:view1];
}
[UIView commitAnimati*****];
}
 
Table View <UITableViewDateSource>
#pragma mark -
#pragma mark Table View Data Source Methods
//指定分区中的行数,默认为1
-(NSInteger)tableView:(UITableView*)tableView 
 numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
 
//设置每一行cell显示的内容
- (UITableViewCell*)tableView:(UITableView*)tableView 
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *SimpleTableIndentifier=@"SimpleTableIndentifier";
UITableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:SimpleTableIndentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] 
initWithStyle:UITableViewCellStyleSubtitle 
reuseIdentifier:SimpleTableIndentifier] 
autorelease];
}
     UIImage*image =[UIImage imageNamed:@"13.gif"];
cell.imageView.image = image;
    
NSUInteger row = [indexPath row];
cell.textLabel.text = [listDataobjectAtIndex:row];
    cell.textLabel.font =[UIFontboldSystemFontOfSize:20];
 
     if(row< 5)
cell.detailTextLabel.text = @"Bestfriends";
else 
    cell.detailTextLabel.text=@"friends";
return cell;
}

图像、文本标签和详细文本标签

图像:如果设置图像,则它显示在文本的左侧;文本标签:这是单元的主要文本(UITableViewCellStyleDefault 只显示文本标签);详细文本标签:这是单元的辅助文本,通常用作解释性说明或标签

UITableViewCellStyleSubtitle
UITableViewCellStyleDefault
UITableViewCellStyleValue1
UITableViewCellStyleValue2
 
<UITableViewDelegate>
#pragma mark -
#pragma mark Table View Delegate Methods
//把每一行缩进级别设置为其行号
- (NSInteger)tableView:(UITableView*)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSUInteger row = [indexPath row];
return row;
}
//获取传递过来的indexPath值
- (NSIndexPath *)tableView:(UITableView*)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0) 
return nil;
return indexPath;
}
 
- (void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSUInteger row = [indexPath row];
NSString *rowValue = [listDataobjectAtIndex:row];
NSString *message = [[NSString alloc]initWithFormat:@"You selected%@",rowValue];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"RowSelected"
message:message
    delegate:nil
  cancelButtonTitle:@"Yes, Idid!"
  otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];
}
 
//设置行的高度
- (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}

NavigationController 推出push 推出pop
[self.navigationControllerpushViewController:_detailControlleranimated:YES];
[self.navigationControllerpopViewControllerAnimated:YES];
 
Debug:
NSLog(@"%s %d", __FUNCTION__,__LINE__);
 
点击textField外的地方回收键盘

先定义一个UIControl类型的对象,在上面可以添加触发事件,令SEL实践为回收键盘的方法,最后将UIControl的实例加到当前View上。
UIControl *m_control = [[UIControlalloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[m_controladdTarget:selfaction:@selector(keyboardReturn) 
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_control];
 
- (void) keyboardReturn
{
[aTextField resignFirstResponder];
}
 
键盘覆盖输入框
当键盘调出时将输入框覆盖时,可以用下方法:
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField
{
[self.view setFrame:CGRectMake(0, -100,320, 480)];
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
  [self.view setFrame:CGRectMake(0, 0,320,480)];
          return YES;
}
当准备输入时,将视图的位置上调100,这样键盘就不能覆盖到输入框。
 
当依赖注入方法不好使时,可以在AppDelegate内申明一个全局的控制器实例_anotherViewController,在另一个需要使用_anotherViewController的地方定义以下委托方法,使用共享的UIApplication实例来获取该委托的引用
SomeAppDelegate *appDelegate =(SomeAppDelegate *)[[UIApplicationsharedApplication] delegate];
_anotherViewController=appDelegate._anotherViewController; 

UIViewController内建TableView

纯代码在UIViewController控制器内建Table View
@interface RootViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> {
NSArray *timeZoneNames;
}
@property (nonatomic,retain)NSArray*timeZoneNames;
@end
 
(void) loadView
{
UITableView *tableView = [[UITableViewalloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]]style:UITableViewStylePlain];
tableView.autoresizingMask =(UIViewAutoresizingFlexibleHeight |UIViewAutoresizingWidth);
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
 
self.view = tableView;
[tableView release];
}
 
 
plist文件中的数据赋给数组
NSString *thePath = [[NSBundle mainBundle]pathForResource:@"States"ofType:@"plist"];
NSArray *array =[NSArrayarrayWithContentsOfFile:thePath];