一. UIView子类中动态创建UISegmentedControl按钮
//TestView.h
@interface TestView :UIView
{
UISegmentedControl *_btnZoomBig;
UISegmentedControl *_btnZoomSmall;
}
@property(nonatomic,retain)UISegmentedControl *btnZoomBig;
@property(nonatomic,retain)UISegmentedControl *btnZoomSmall;
- (IBAction)OnZoomBig:(id)sender;
- (IBAction)OnZoomSmall:(id)sender;
@end
//TestView.m
@implementation TestView
@synthesize btnZoomBig =_btnZoomBig;
@synthesize btnZoomSmall =_btnZoomSmall;
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
[selfinitViews:frame];
}
returnself;
}
- (void)dealloc
{
[_btnZoomBigrelease];
[_btnZoomSmallrelease];
[superdealloc];
}
- (void)initViews:(CGRect)frame
{
@autoreleasepool {
NSDictionary *dictionnary = [NSDictionarydictionaryWithObjectsAndKeys:
[UIColorcolorWithRed:220.0/255.0green:104.0/255.0blue:1.0/255.0alpha:1.0],UITextAttributeTextColor,
[UIColorcolorWithRed:1.0green:1.0blue:1.0alpha:1.0],UITextAttributeTextShadowColor,
[NSValuevalueWithUIOffset:UIOffsetMake(0,1)],UITextAttributeTextShadowOffset,
[UIFontboldSystemFontOfSize:40],UITextAttributeFont,
nil];//按钮属性(字体颜色、阴影、字体大小)
NSArray *array = [NSArrayarrayWithObject:@"+"];
self.btnZoomBig = [[[UISegmentedControlalloc]initWithItems:array]autorelease];
NSArray *array1 = [NSArrayarrayWithObject:@"-"];
self.btnZoomSmall = [[[UISegmentedControlalloc]initWithItems:array1]autorelease];
self.btnZoomBig.tintColor = [UIColorgrayColor];
self.btnZoomBig.segmentedControlStyle =UISegmentedControlStyleBar;
self.btnZoomBig.momentary =YES;
self.btnZoomBig.alpha =0.6;
[self.btnZoomBigsetTitleTextAttributes:dictionnaryforState:UIControlStateNormal];
[self.btnZoomBigaddTarget:selfaction:@selector(OnZoomBig:)forControlEvents:UIControlEventValueChanged];
self.btnZoomSmall.tintColor = [UIColorgrayColor];
self.btnZoomSmall.segmentedControlStyle =UISegmentedControlStyleBar;
self.btnZoomSmall.momentary =YES;
self.btnZoomSmall.alpha =0.6;
[self.btnZoomSmallsetTitleTextAttributes:dictionnaryforState:UIControlStateNormal];//设置按钮标题属性
[self.btnZoomSmalladdTarget:selfaction:@selector(OnZoomSmall:)forControlEvents:UIControlEventValueChanged];//触发事件
[selfaddSubview:self.btnZoomBig];
[selfaddSubview:self.btnZoomSmall];
}
}
- (void)layoutSubviews
{
CGRect frame = [selfbounds];
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
if (width>height) {
CGFloat dx = width/6;
self.btnZoomBig.frame =CGRectMake(0,0, dx, height);
self.btnZoomSmall.frame =CGRectMake(dx,0, dx, height);
}
else {
CGFloat dy = height/6;
self.btnZoomBig.frame =CGRectMake(0,0, width, dy);
self.btnZoomSmall.frame =CGRectMake(0, dy, width, dy);
}
}
- (IBAction)OnZoomBig:(id)sender
{
}
- (IBAction)OnZoomSmall:(id)sender
{
}
@end
二. 创建模态对话框窗口
NSRect rect = NSMakeRect(20, 20, 200, 200);
NSUInteger windowStyle = NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask;
NSWindow *window = [[NSWindow alloc] initWithContentRect:rect styleMask:windowStyle backing:NSBackingStoreBuffered defer:NO];
[NSApp runModalForWindow: window];
三. Wifi支持
使用Wi-Fi
如果您的应用程序通过Wi-Fi无线信号访问网络,则必须将这个事实通知系统,即在应用程序的Info.plist
文件中包含UIRequiresPersistentWiFi
键。包含这个键使系统知道在检测到活动的Wi-Fi 热区时应该弹出网络选择框,同时还使系统知道在您的应用程序运行时不应试图关闭Wi-Fi硬件。
为了防止Wi-Fi硬件消耗太多的电能,iPhone OS内置一个定时器,如果在30分钟内没有应用程序通过UIRequiresPersistentWiFi
键请求使用Wi-Fi,就会完全关闭该硬件。如果用户启动某个包含该键的应用程序,则在该程序的生命周期中,iPhone OS会有效地禁用该定时器。但是一旦该程序退出,系统就会重新启用该定时器。
请注意:即使UIRequiresPersistentWiFi
键的值为true
,在设备空闲(也就是处于屏幕锁定状态)时也是没有效果的。在那种情况下,应用程序被认为是不活动的,虽然它可能在某些级别上还在工作,但没有Wi-Fi连接。
int value =1;//0 close
int error = setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (const void *)&value, sizeof(value));
SOCKET s,
char* buf,
int len,
int flags,
struct sockaddr* from, //这个参数里就是发送人的地址结构
int* fromlen //sizeof(*from)
);
如果想得到发送方的ip: "192.168.0.100 "可用char *inet_ntoa (struct in_addr in);
如:
int err;
int sock;
uint8_t buffer[1024];
ssize_t bytesRead;
struct sockaddr_in from;
int fromlen=sizeof(from);
if (self->_cfInSocket==NULL) {
return;
}
sock =CFSocketGetNative(self->_cfInSocket);
assert(sock >=0);
bytesRead =recvfrom(sock, buffer,sizeof(buffer),0, &from, &fromlen);
if (bytesRead <0)
{
err =errno;
}
elseif (bytesRead ==0)
{
err =EPIPE;
}
else
{
char ip[16];
NSUInteger port;
strcpy(ip,inet_ntoa(from.sin_addr)); //from即为上面recvfrom里的参数
port=ntohs(from.sin_port);
}
六.UDP收和发socket绑定同一个固定端口
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
七. 在Mac 32-bit编译环境下,变量-属性的声明和定义
@interface AppDelegate :NSObject <NSApplicationDelegate>
{
NSWindow *_window; //必须声明_window变量
}
@property (assign)IBOutlet NSWindow *window;
@end
@implementation AppDelegate
@synthesize window=_window; //window 属性为 _window 实例变量合成访问器方法
-(void)dealloc
{
[_windowrelease];
[superdealloc];
}
@end
Synthesized property 'window' must either be named the same as a compatible ivar or must explicitly name an ivar
在 Mac 64-bit编译环境下,编译器会自动给属性window添加一个下划线"_"做前缀的实例变量(ivar) _window。
NSData *my_nsdata = (__bridge_transfer NSData*)my_cfdata; // -1 on the my_cfdata
NSData *my_nsdata = (__bridge_transfer NSData*)my_cfdata; // -1 on the my_cfdata
or
NSData *my_nsdata = (__bridge NSData*)my_cfdata; // no adjustment of retain counts
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
用过UIScrollView的都会发现UIScrollView不会响应touch事件,这样就无法在touchesEnd方法中做一些事情了,比如关闭键盘等等。其实写个category就可以解决这个问题了
@implementation UIScrollView (UITouchEvent)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
@end
十.单击UIView的非UIEditField区域,隐藏模拟键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.hostAddr resignFirstResponder];//hostAddr是UITextField的属性实例变量。
}
十一.iOS获得本机IP地址
//获得本机名称
- (NSString *) hostname
{
char baseHostName[256];// Thanks, Gunnar Larisch
int success =gethostname(baseHostName, 255);
if (success != 0)returnnil;
baseHostName[255] = '\0';
#if TARGET_IPHONE_SIMULATOR
return [NSStringstringWithFormat:@"%s", baseHostName];
#else
return [NSString stringWithFormat:@"%s.local", baseHostName];
#endif
}
//通过hostname获得本地的IP地址
- (NSString *) localIPAddress
{
//将本机名字转化成主机网络结构体struct hostent
structhostent *host = gethostbyname([[selfhostname]UTF8String]);
if (!host) {herror("resolv");returnnil;}
structin_addr **list = (structin_addr **)(host->h_addr_list);
return [NSStringstringWithCString:(constchar*)inet_ntoa(*list[0])encoding:NSUTF8StringEncoding];
}
十二.Implicit declaration of function 'inet_aton' is invalid in C99
解决办法:-std=gnu99的编译选项,#import <arpa/inet.h>。
十三.获得本机的IP地址
#import <ifaddrs.h>
#import <arpa/inet.h>
- (NSString *)getIPAddress
{
NSString *address =@"error";
structifaddrs *interfaces =NULL;
structifaddrs *temp_addr =NULL;
int success =0;
// retrieve the current interfaces - returns 0 on success
success =getifaddrs(&interfaces);
if (success ==0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while (temp_addr !=NULL) {
if( temp_addr->ifa_addr->sa_family ==AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[NSStringstringWithUTF8String:temp_addr->ifa_name]isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSStringstringWithUTF8String:inet_ntoa(((structsockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
十四.修改UITextField的高度
修改NIB资源文件*.xib的文件属性, Show the File inspector->Use Autolayout。
选择UITextField->Editor->Pin->Height
十五.获得UIView对象的所有者UIViewController
id object = [selfnextResponder];
if ([objectisKindOfClass:[UIViewControllerclass]]) {
UIViewController *viewController = (UIViewController*)object;
}
elseif (object isKindOfClass:[UIView class]) {
UIView *view = (UIView*)object;
}
十六.iPhone模拟器在MAC系统中的文件路径
先在Finder中显示隐藏文件夹/文件,
其中#username#表示当前用户名:
/Users/#username#/Library/Application Support/iPhone Simulator/User/Applications/
十七、XCode 5 deployment target limited only to iOS 7
project -> Build Settings->Deployment-> iOS Deployment Target
十九、修改iOS应用App名称
Build Settings->Packaging->Product Name 修改Product Name