IOS开发知识片段


1:获取键盘视图

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if(([[keyboard description] hasPrefix:@" CGRect frame = CGRectMake(0.0f, 162.0f, 106.0f, 53.0f);
if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)){
frame = CGRectMake(0.0f, 116.0f, 162.0f, 53.0f);
}
[doneBt setFrame:frame];
[keyboard addSubview:doneBt];
break;
}

}


2:获取iPhone内存大小

首先在头文件中包括

- (double)availableMemory {

vm_statistics_data_t vmStats;

mach_msg_type_number_t infoCount =HOST_VM_INFO_COUNT;

kern_return_t kernReturn =host_statistics(mach_host_self(),HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);

if(kernReturn !=KERN_SUCCESS) {

returnNSNotFound;

}

return ((vm_page_size * vmStats.free_count) /1024.0) / 1024.0;

}


3:判断iphone当前所用网络类型

//***************檢查網路狀態******************

struct sockaddr_in Addr;

bzero(&Addr,sizeof(Addr));

Addr.sin_len =sizeof(Addr);

Addr.sin_family =AF_INET;

SCNetworkReachabilityRef target =SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &Addr);

SCNetworkReachabilityFlags flags;

SCNetworkReachabilityGetFlags(target, &flags);

CFRelease(target);

    

BOOL B=(BOOL)kSCNetworkReachabilityFlagsIsWWAN;

BOOL A=kSCNetworkFlagsReachable;

BOOL C=flags;

NSLog(@"Wi-Fi=%i, 3G=%i,網路=%i",A,B,C);

//無線網路

if (flags &kSCNetworkFlagsReachable)

{

if (flags &kSCNetworkReachabilityFlagsIsWWAN)

{

netstatus.text =@"WWAN";

NSLog(@"WWAN");

}

else {

netstatus.text =@"Wi-Fi";

NSLog(@"Wi-Fi");

}

}

else {

netstatus.text = @"Offline";

NSLog(@"Offline");

}


4:自定义UIActionSheet

-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title

{//height = 84, 134, 184, 234, 284, 334, 384, 434, 484

self = [superinit];

    if (self

{

int theight = height -40;

int btnnum = theight/50;

for(int i=0; i

{

[selfaddButtonWithTitle:@" 1"];

}

toolBar = [[UIToolbaralloc]initWithFrame:CGRectMake(0,0,320,44)];

toolBar.barStyle = UIBarStyleDefault;

UIBarButtonItem *titleButton = [[UIBarButtonItemalloc]initWithTitle:titlestyle:UIBarButtonItemStylePlaintarget:nilaction:nil];

UIBarButtonItem *rightButton = [[UIBarButtonItemalloc]initWithTitle:@"Done"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(done)];

UIBarButtonItem *leftButton  = [[UIBarButtonItemalloc]initWithTitle:@"Cancel"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(docancel)];

UIBarButtonItem *fixedButton  = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];

NSArray *array = [[NSArrayalloc]initWithObjects: leftButton,fixedButton, titleButton,fixedButton, rightButton,nil];

[toolBarsetItems: array];

[titleButtonrelease];

[leftButton release];

[rightButtonrelease];

[fixedButtonrelease];

[array      release];

[selfaddSubview:toolBar];

view = [[UIViewalloc]initWithFrame:CGRectMake(0,44,320, height-44)];

view.backgroundColor = [UIColorgroupTableViewBackgroundColor];

[selfaddSubview:view];

    }

   returnself;

}

-(void)done

{

[selfdismissWithClickedButtonIndex:0animated:YES];

}

-(void)docancel

{

[selfdismissWithClickedButtonIndex:0animated:YES];

}



5:移除一个UIView中的button

for (UIView *view in cell.subviews)

{

if ([view isKindOfClass [UIButton class]])

{

[view removeFromSupview];

}

}







你可能感兴趣的:(iPhone)