2015年5月11日 星期一

[iOS] 可輸入文字(textfield)之警告視窗(alertview)

又遇到問題,於是找了方法來解決


android上面的alertview十分簡單(custom的部份)

iOS呢?是否一樣簡單?

[ iOS 7 + Xcode 6.3.1 ]


第一次看別人的code:


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"輸入IP" message:@" " delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確認",nil];
    UITextField * txt = [[UITextField alloc] init];
    txt.backgroundColor = [UIColor whiteColor];
    txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
    [alert addSubview:txt];

    [alert show];

結果有跳出警告視窗,但是沒有可以輸入的阿!

於是就繼續查,才發現要加上

alert.alertViewStyle = UIAlertViewStylePlainTextInput;


正確版本:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"輸入IP" message:@" " delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確認",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * txt = [[UITextField alloc] init];
    txt.backgroundColor = [UIColor whiteColor];
    txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
    [alert addSubview:txt];
    [alert show];

當然style也有password/default(無法顯示)/login..之類的,大家可以自行嘗試喔!

end

2015年5月7日 星期四

[iOS] 改變文字顏色,無效?解決方式

在調整UI的時候常常需要改變顏色

可是使用[UIColor colorwithRed:  green: blue: alpha: ]為什麼輸入數值都沒有反應呢?


錯誤示範:[UIColor colorwithRed: 24 green:116 blue:250  alpha:1 ]

正確示範:[UIColor colorWithRed:24.0f/255.0f green:116.0f/255.0f blue:205.0f/255.0f alpha:1.0f];

需要除以255而且他是float喔!

改完以後就會正確顯示顏色了,簡單但是不知道會弄很久的功能!


end

2015年5月6日 星期三

[iOS] Xcode裡面xib檔內的物件改名後產生NSUnknowkeyException的錯誤解決方法

今天在做ios app的時候又發生了一個悲劇....


看到.h檔裡面的英文拼錯,於是就順手改一下...然後就error!!


找了很久以後,才發現錯誤出現在這邊------



看到Referencing Outlets這邊,

這邊是已經修改過的,如果錯誤的話應該會reference到兩個一個是拼錯字的

另一個才是正確的,我們需要把錯誤的按一下連線的那邊有個x,按下去以後就解除reference

這樣錯誤就修改完成囉!


End

2015年4月30日 星期四

[iOS] 如何讓object型態轉換為NSString並顯示在UI上?

今天又遇到讓我卡很久的難題了,一樣把它記錄起來以免日後忘記 .


首先.h檔跟storyboard(或xib檔)連結,h file長這樣:


#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface Page1 : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *in_volt;
@property (weak, nonatomic) IBOutlet UILabel *out_volt;
@property (weak, nonatomic) IBOutlet UILabel *load;
@property (weak, nonatomic) IBOutlet UILabel *bat_level;
@property (weak, nonatomic) IBOutlet UILabel *bat_backup_time;
@property (weak, nonatomic) IBOutlet UILabel *ups_status;
@property (weak, nonatomic) IBOutlet UILabel *bat_stauts;
@property (weak, nonatomic) IBOutlet UILabel *power_condition;


@end


step1:

第一步是要讓UILable可以顯示資料

所以很簡單,在要觸發的地方寫下self.(UILabel名稱).text = (nsstring型態);

好了,很顯然第一個問題就是,我們現在是object型態,該怎樣轉NSString呢?

step2:

因為要顯示的是數字,所以先把它轉為int

object to int :

[(NSString *)object  integerValue]

這樣就變成int型態囉!

step3:

轉int還是不能顯示在UILabel上阿!

所以我們還需要再轉一次,這時候使用

[NSString stringWithFormat:@"%d", (int型態)]

但是呢!Xcode會跟你說warning,所以要改成

NSString stringWithFormat:@"%ld", (強制轉換型態成long,就是前面加個(long)這樣)]

完成就長這樣:

self.in_volt.text = [NSString stringWithFormat:@"%ld", (long)[(NSString *)你填入的object型態在這邊 integerValue]];

完成!

end

2015年4月29日 星期三

[iOS] Tab Bar分頁+UDP通訊

不知道怎樣寫分頁或是怎樣做UDP通訊嗎??

最近好不容易弄出了一點樣子,把它記錄起來,以免以後忘記.

首先分頁是使用tab bar元件-GG Tab Bar(https://github.com/Goles/GGTabBar)

,讓分頁切成四塊(同時建立四個.h+.m+.xib檔案)

可以看到有分了Page1,2,3,4分頁,以及對應的xib檔


首先,先在各個page.m裡面新增:


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil
                                                        image:[UIImage imageNamed:@"user_normal"]
                                                selectedImage:[UIImage imageNamed:@"user_pressed"]];
    }
    return self;
}

(image的部份就是要自己找圖或是下載source code,名稱自行調整,但需要對應到Images.xcassets裡面的檔案名稱)


step2: AppDelegate.h+m的部份

AppDelegate.h

#import <UIKit/UIKit.h>

#import "AsyncUdpSocket.h"
#define global ((AppDelegate *)[[UIApplication sharedApplication] delegate])

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    AsyncUdpSocket *udpSocket;
    NSTimer *timer;
    long tag;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,retain) NSMutableArray *globalData;


@end


AppDelegate.m


#import "AppDelegate.h"
#import "GGTabBarController.h"
#import "Page1.h"
#import "Page2.h"
#import "Page3.h"
#import "Page4.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    NSLog(@"open");
    
    global.globalData = [[NSMutableArray alloc] initWithCapacity:300];
    
    udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
    NSError *error = nil;
    [udpSocket bindToPort:2601 error:&error]; //綁定一個port 讓發送的source port / 指定接收的port 都是這一個
    [udpSocket receiveWithTimeout:-1 tag:0];//Start listening for a UDP packet.
    timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(sender) userInfo:nil repeats:YES];
    
    GGTabBarController *tabBar = [[GGTabBarController alloc] init];
    
    Page1 *vc1 = [[Page1 alloc] init];
    Page2 *vc2 = [[Page2 alloc] init];
    Page3 *vc3 = [[Page3 alloc] init];
    Page4 *vc4 = [[Page4 alloc] init];
    
    tabBar.viewControllers = @[vc1, vc2, vc3, vc4];
    self.window.rootViewController = tabBar;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

//Server端 接收
-(BOOL) onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
    //NSLog(@"in server");
    Byte *convert_to_byte = (Byte *)[data bytes];
    int length = data.length;
    //NSLog(@"length = %d",length);
    int receive[length];
    NSNumber *number;
    NSMutableArray *intArray = [[NSMutableArray alloc] init];
    
    for (int i = 0 ; i < [data length]; i++) {
        //printf("conver to byte = %d\n",convert_to_byte[i]);
        int combine = (int)convert_to_byte[i];
        //NSLog(@"%ld",combine);
        receive[i] = combine;
        number =[NSNumber numberWithInt:combine];
        [intArray addObject:number];
        NSLog(@"number =%@",number);
        //[global.globalData addObject:number];
        //NSLog(@"i=%@",global.globalData[i]);
        
        NSLog(@"%d",receive[i]);//詳細接收
    }
    [global.globalData setArray:intArray];
    [intArray removeAllObjects];
    
    for(int i = 0; i < global.globalData.count; i++){
        NSLog(@"i=%@",[global.globalData objectAtIndex:i]);
    }
    
    NSLog(@"global-data count= %lu",(unsigned long)global.globalData.count);
    
    //receive要解析,
    [udpSocket sendData:data toHost:host port:port withTimeout:-1 tag:0];
    [udpSocket receiveWithTimeout:-1 tag:0];
    return YES;
}


-(void) sender{
    //NSLog(@"timer");
    const unsigned char byte[] = {80,67,77,71,1,69,78,68};
    NSData *data = [NSData dataWithBytes:byte length:sizeof(byte)];
    [udpSocket sendData:data toHost:@"210.202.53.147" port:2601 withTimeout:-1 tag:tag];
}

@end

step3 : 建立udp通訊

這部份在前幾篇有講到附上網址:udp通訊part

(不過這部份已經有寫在上面了)



做完通訊,又有tab bar分頁功能以後,就是把分頁內容建立好囉!

教學到此結束,end.