[iOS] Adding UITableView to ViewController

  1. Put tableView to view controller and link it to viewcontroller.
  2. link delegate and datasource of tableview to viewcontroller.
  3. inviewcontroller.h
     @interface ViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
  4. in viewDidLoad put
     tblService.delegate=self;
     tblService.dataSource=self;

     


而在.m檔中,必須要實作下列三個方法。第一個方法是設定Tableview裡面有多少區段。


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    // Return the number of sections.
    return 1;
}

這個方法是設定在這個UITableView裡面有幾個區段,區段我們看到下圖。在iPhone設定畫面上,會看到相似的功能被歸相同的群組裡面。這部分就是區段。所以要設定有多少區段,必須從這個方法來設定。


這裡是設定每一個區段有多少筆資料。


- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
    return [array count];
}

這個方法是用來Rander畫面上Cell欄位資料。

在這個方法中,在程式中static NSString *CellIdentifier = @”Cell”;

對UITable Cell的命名和Storyboard 裡的設定值,如果兩邊名稱不一樣,會導致你的UITable Cell資料會顯示不出來。

設定 Cell 的 identifier:

 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"Cell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    //Configure the cell.
    //cell.textLabel.text = [self.colorNames objectAtIndex:[indexPath row]];
    cell.textLabel.text = [array objectAtIndex:[indexPath row]];
    return cell;
}


相關文章:

如何在普通 UIViewController 中使用 UITableView
https://lvwenhan.com/ios/429.html
[iOS]UITableView的基本觀念
https://dotblogs.com.tw/toysboy21/2013/11/19/130485

 

[iOS] NSArray與NSMutableArray與NSMutableDictionary
http://stackoverflow.max-everyday.com/2017/05/nsarray-nsmutablearray-nsmutabledictionary/

 


from:
http://stackoverflow.com/questions/38489653/adding-uitableview-to-viewcontroller

[iOS] for each loop in objective c for accessing NSMutable dictionary

如何瀏覽所有NSMutableDictionary 裡的內容?

NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init];

解答:

for (NSString* key in xyz) {
    id value = [xyz objectForKey:key];
    // do stuff
}

This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary is one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumeration in the Collections Programming Topic.

Oh, I should add however that you should NEVER modify a collection while enumerating through it.

 


from:

http://stackoverflow.com/questions/2143372/for-each-loop-in-objective-c-for-accessing-nsmutable-dictionary

[iOS] NSArray與NSMutableArray與NSMutableDictionary

NSArray:固定長度陣列

使用固定一串資料給NSArray時,必須在陣列最後一個值放入nil,否則會發生錯誤。


範例:
NSArray *array = [ [ NSArray alloc ] initWithObjects:@”aa”,@”bbb”,nil];//宣告一陣列放入aa、bb字串
NSLog(@”array count==%d”,array.count);//印出陣列長度

for(int i = 0; i < array.count; i++){//取出陣列裡的字串並印出來
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];//此陣列為暫存,且之後沒用到所以就將陣列從記憶體釋放
=================================================================
NSMutableArray:動態陣列

可以不斷放入物件的陣列,陣列長度隨著放入的物件變動
靜態宣告陣列時,一樣必須在陣列最後一個值放入nil,否則會發生錯誤。


範例1 (動態新增):

NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:@”aa”];
[array addObject:@”bbb”];

NSLog(@”array count==%d”,array.count);

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];


範例2 (靜態放入值+動態新增)

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@”cc”,@”ddd”,nil];
[array addObject:@”aa”];
[array addObject:@”bbb”];

NSLog(@”array count==%d”,array.count);

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];


範例3 指定index放入物件,使用insertObject指定位置時,指定的位置必須是 (陣列長度 – 1) 以內的範例值

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@”cc”,@”ddd”,nil];
[array addObject:@”aa”];
[array addObject:@”bbb”];

[array insertObject:@”aaaa” atIndex:4]; //在位置4放入 字串 aaaa


範例4:移除物件

移除指定位置物件
[array removeObjectAtIndex:4];

清除所有物件
[array removeAllObjects];


範例5:將數值放入陣列

int percentage = 40;

// 產生一個NSNumber物件,可以用signed or unsigned char, short int, int, long int, long long int, float, double or BOOL等基本型態產生物件
NSNumber *percentageObject = [NSNumber numberWithFloat:percentage];

//將NSNumber物件放入array
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:percentageObject];

//取出數值
[percentageObject intValue];


範例6:將指定位置的物件替換掉==>replaceObjectAtIndex:索引值(int) withObject:物件(id)

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@”cc”,@”ddd”,nil];
[array addObject:@”aa”];
[array addObject:@”bbb”];

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}

[array replaceObjectAtIndex:2 withObject:@”111″];

for(int i = 0; i < array.count; i++){
NSLog(@”i=%@”,[array objectAtIndex:i]);
}
[array release];
NSDictionary及NSMutableDictionary(與java的Hashtable相似
============================================================

NSDictionary及NSMutableDictionary兩種,兩者合稱字典(dictionary),Mutable–善變的–表示可以變,NSDictionary則像是constant。NSMutableDictionary是NSDictionary的subClass。這就像一本字典,概念是(key, value),用key來搜尋出所需的value。

NSDictionary:

initWithObjectsAndKeys:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: @”one”, [NSNumber numberWithInt: 1], @”two”,[NSNumber numberWithInt: 3], nil];

-(id) initWithObjectsAndKeys:(id) firstObject, ….

…..

第一個是firstObject的key,接著是secondObject、key,之後即為object、key的配對,直到出現nil為止。

注意:事實上nil一定出現在object的位置。若key是nil,會產生NSInvalidArgumentException。

NSMutableDictionary:

objectForKey:以下的key是由key = [objectOfNSEnumerator nextObject]; 產生的

[[[objectOfNSMutableDictionary objectForKey:key ] description] cString];

setObject: forKey:[mutable setObject:@”Tom” forKey:@”[email protected]”];

範例1: 利用NSEnumerator 取出NSMutableDictionary陣列裡所有的物件,

(1):取得所有value
NSMutableDictionary *taiStyle = [[NSMutableDictionary alloc]init];
//所有台型字串
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”102″]; //
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”103″]; //

NSEnumerator *enumerator = [taiStyle objectEnumerator];
id value;

while ((value = [enumerator nextObject])) {
NSLog(@”%i”,[((NSNumber*)value) intValue]);
}

(2):取得所有key
NSMutableDictionary *taiStyle = [[NSMutableDictionary alloc]init];
//所有台型字串
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”102″]; //
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”103″]; //

NSEnumerator *enumerator = [taiStyle keyEnumerator];
id key;

while ((key = [enumerator nextObject])) {
NSLog(@”%@”,((NSString*)key));
}


範例2:
(1)用key取出value物件,使用 valueForKey: key ,若使入的key找不到對應的key,會回傳nil

[[NSMutableDictionary *taiStyle = [[NSMutableDictionary alloc]init];

[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”102″]; //
[taiStyle setObject:[[NSNumber alloc] initWithInt:1] forKey:@”103″]; //

NSString *key = @”102″;
NSNumber *vlaue = [taiStyle valueForKey:key];
NSLog(@”%i”,[vlaue intValue] );
(2)指定key移除對應的key與value物件
[taiStyle removeObjectForKey:@”102″];//移除以@”102″字串當key的物件,key也會被移除

[iOS] UIActionSheet 的使用方法

UIActionSheet 是一個由下往上彈出的選單.

在使用 UIActionSheet 之前,請先為您的程式加上代理

@interface UIActionSheetViewController : UIViewController<UIActionSheetDelegate> {
 
}

在按鈕事件加入產生 UIActionSheet 的程式碼。

 UIActionSheet *actionSheet = [[UIActionSheet alloc] 
 initWithTitle:@"title,nil时不显示" 
 delegate:self 
 cancelButtonTitle:@"取消" 
 destructiveButtonTitle:@"确定" 
 otherButtonTitles:@"第一项", @"第二项",nil]; 
 actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
 [actionSheet showInView:self.view];

 

//也可以透過此方式新增按鈕

[actionSheet addButtonWithTitle:@”NewButton”];

 

所觸發的按鈕事件寫法就與 Alerts 一模一樣了,同都是呼叫一個內建函式,使用索引 Index 並配合按鈕標題來做判斷。

//判斷ActionSheet按鈕事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
 
 //將按鈕的Title當作判斷的依據
 NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
 
 if([title isEqualToString:@"Title1"]) {
 // code here.
 }
}

和 UIActionSheet 類似的是

 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"选择" message:@"请选择一个数!" delegate:weakSelf cancelButtonTitle:@"取消" otherButtonTitles:nil];
 for (int index = 0; index < 100; index++) {
 [alertView addButtonWithTitle:[NSString stringWithFormat:@"%i", index]];
 }
 [alertView show];

 

UIAlertView有如下4中樣式:

– UIAlertViewStyleDefault 默認樣式,沒有熟人框
– UIAlertViewStyleSecureTextInput 有一個密碼輸入框
– UIAlertViewStylePlainTextInput 有一個普通文本輸入框
– UIAlertViewStyleLoginAndPasswordInput 有一個普通文本輸入框和一個密碼輸入框


UIAlertView有如下幾個代理方法
在點擊按鈕的時候觸發,buttonIndex為點擊的按鈕序號
-(void)alertView:(UIAlertView* )alertView clickedButtonAtIndex:(NSInteger)buttonIndex

在將要顯示的時候觸發
-(void)willPresentAlertView:(UIAlertView )alertView

在已經顯示的時候觸發
-(void)didPresentAlertView:(UIAlertView )alertView

在將要消失的時候觸發
-(void)alertView:(UIAlertView )alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

在已經消失的時候觸發
-(void)alertView:(UIAlertView )alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

在編輯文本框後觸發
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView )alertView NS_DEPRECATED_IOS(2_0, 9_0);

 

 

執行出來的效果:


相關文章:

iOS 弹出框总结
http://www.jianshu.com/p/4d1ca0d9a6f1

 

[iOS] How do I save user preferences for my iPhone app?

One of the easiest ways would be saving it in the NSUserDefaults:

Setting:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:value 
                 forKey:key];
// – setBool:forKey:
// – setFloat:forKey:  
// in your case 
[userDefaults synchronize];

Getting:

[[NSUserDefaults standardUserDefaults] objectForKey:key];

 boolForKey:

[iOS] 正規表達式 (Regular Expression)

台灣的身份證字號 regular expression:

    NSString *errorMessage;
    // 確認身分證字號格式
    NSString *phoneNumber = @“L123456789";
    NSString *phoneRegex = @"[A-Z][0-9]{9}";
    NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    BOOL matches = [test evaluateWithObject:phoneNumber];
    if (!matches) {
        errorMessage = @"身份證字號第一碼英文+後9碼為數字";
    }

http://stackoverflow.com/questions/3349494/objective-c-regex-to-check-phone-number

How to validate a phone number (NSString *) in objective-c? Rules:

minimum 7 digits
maximum 10 digits
the first digit must be 2, 3, 5, 6, 8 or 9

You can use a regular expressions library (like RegexKit, etc), or you could use regular expressions through NSPredicate (a bit more obscure, but doesn’t require third-party libraries). That would look something like this:

NSString *phoneNumber = ...;
NSString *phoneRegex = @"[235689][0-9]{6}([0-9]{3})?"; 
NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex]; 
BOOL matches = [test evaluateWithObject:phoneNumber];

If you’re on iPhone, then iOS 4 introduced NSRegularExpression, which would also work. The NSPredicate approach works on both Mac and iPhone (any version).


http://stackoverflow.com/questions/5478170/regular-expression-in-ios

I am looking for a regular expression to match the following -100..100:0.01. The meaning of this expression is that the value can increment by 0.01 and should be in the range -100 to 100.

You could use NSRegularExpression instead. It does support \b, btw, though you have to escape it in the string:

NSString *regex = @"\\b-?1?[0-9]{2}(\\.[0-9]{1,2})?\\b";

Though, I think \\W would be a better idea, since \\b messes up detecting the negative sign on the number.

A hopefully better example:

NSString *string = <...your source string...>;
NSError  *error  = NULL;

NSRegularExpression *regex = [NSRegularExpression 
  regularExpressionWithPattern:@"\\W-?1?[0-9]{2}(\\.[0-9]{1,2})?\\W"
                       options:0
                         error:&error];

NSRange range   = [regex rangeOfFirstMatchInString:string
                              options:0 
                              range:NSMakeRange(0, [string length])];
NSString *result = [string substringWithRange:range];

Email 格式檢查:

emailRegEx = “[A-Z0-9a-z.-_][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,3}”


首先,特殊符號’^’和’$’。他們的作用是分別指出一個字符串的開始和結束。eg:
「^one」:表示所有以」one」開始的字符串(」one cat」,」one123″,·····);
類似於:- (BOOL)hasPrefix:(NSString *)aString;
「a dog$」:表示所以以」a dog」結尾的字符串(」it is a dog」,·····);
類似於:- (BOOL)hasSuffix:(NSString *)aString;
「^apple$」:表示開始和結尾都是」apple」的字符串,這個是唯一的~;
「banana」:表示任何包含」banana」的字符串。
類似於 iOS8的新方法- (BOOL)containsString:(NSString *)aString,搜索子串用的。

‘*’,’+’和’?’這三個符號,表示一個或N個字符重復出現的次數。它們分別表示「沒有或更多」([0,+∞]取整),「一次或更多」([1,+∞]取整),「沒有或一次」([0,1]取整)。下面是幾個例子:
「ab*」:表示一個字符串有一個a後面跟著零個或若干個b(」a」, 「ab」, 「abbb」,……);
「ab+」:表示一個字符串有一個a後面跟著至少一個b或者更多( 」ab」, 「abbb」,……);
「ab?」:表示一個字符串有一個a後面跟著零個或者一個b( 」a」, 「ab」);
「a?b+$」:表示在字符串的末尾有零個或一個a跟著一個或幾個b( 」b」, 「ab」,」bb」,」abb」,……)。

可以用大括號括起來({}),表示一個重復的具體範圍。例如
「ab{4}」:表示一個字符串有一個a跟著4個b(」abbbb」);
「ab{1,}」:表示一個字符串有一個a跟著至少1個b(」ab」,」abb」,」abbb」,……);
「ab{3,4}」:表示一個字符串有一個a跟著3到4個b(」abbb」,」abbbb」)。
那麼,「*」可以用{0,}表示,「+」可以用{1,}表示,「?」可以用{0,1}表示
注意:可以沒有下限,但是不能沒有上限!例如「ab{,5}」是錯誤的寫法

「 | 」表示「或」操作:
「a|b」:表示一個字符串里有」a」或者」b」;
「(a|bcd)ef」:表示」aef」或」bcdef」;
「(a|b)*c」:表示一串」a」”b」混合的字符串後面跟一個」c」;
方括號」[ ]「表示在括號內的眾多字符中,選擇1-N個括號內的符合語法的字符作為結果,例如
「[ab]「:表示一個字符串有一個」a」或」b」(相當於」a|b」);
「[a-d]「:表示一個字符串包含小寫的’a’到’d’中的一個(相當於」a|b|c|d」或者」[abcd]「);
「^[a-zA-Z]「:表示一個以字母開頭的字符串;
「[0-9]a」:表示a前有一位的數字;
「[a-zA-Z0-9]$」:表示一個字符串以一個字母或數字結束。
「.」匹配除「\r\n」之外的任何單個字符:
「a.[a-z]「:表示一個字符串有一個」a」後面跟著一個任意字符和一個小寫字母;
「^.{5}$」:表示任意1個長度為5的字符串;
「\num」 其中num是一個正整數。表示」\num」之前的字符出現相同的個數,例如
「(.)\1″:表示兩個連續的相同字符。
「10\{1,2\}」 : 表示數字1後面跟著1或者2個0 (「10″,」100″)。
」 0\{3,\} 」 表示數字為至少3個連續的0 (「000」,「0000」,······)。
在方括號里用’^’表示不希望出現的字符,’^’應在方括號里的第一位。
「@[^a-zA-Z][email protected]」表示兩個」@」中不應該出現字母)。
常用的還有:
「 \d 」匹配一個數字字符。等價於[0-9]。
「 \D」匹配一個非數字字符。等價於[^0-9]。
「 \w 」匹配包括下划線的任何單詞字符。等價於「[A-Za-z0-9_]」。
「 \W 」匹配任何非單詞字符。等價於「[^A-Za-z0-9_]」。
iOS中書寫正則表達式,碰到轉義字符,多加一個「\」,例如:
全數字字符:@」^\\d\+$」


1.驗證用戶名和密碼:」^[a-zA-Z]\w{5,15}$」
2.驗證電話號碼:(」^(\\d{3,4}-)\\d{7,8}$」)
eg:021-68686868 0511-6868686;
3.驗證手機號碼:」^1[3|4|5|7|8][0-9]\\d{8}$」;
4.驗證身份證號(15位或18位數字):」\\d{14}[[0-9],0-9xX]」;
5.驗證Email地址:(「^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\.\\w+([-.]\\w+)*$」);
6.只能輸入由數字和26個英文字母組成的字符串:(「^[A-Za-z0-9]+$」) ;
7.整數或者小數:^[0-9]+([.]{0,1}[0-9]+){0,1}$
8.只能輸入數字:」^[0-9]*$」。
9.只能輸入n位的數字:」^\\d{n}$」。
10.只能輸入至少n位的數字:」^\\d{n,}$」。
11.只能輸入m~n位的數字:」^\\d{m,n}$」。
12.只能輸入零和非零開頭的數字:」^(0|[1-9][0-9]*)$」。
13.只能輸入有兩位小數的正實數:」^[0-9]+(.[0-9]{2})?$」。
14.只能輸入有1~3位小數的正實數:」^[0-9]+(\.[0-9]{1,3})?$」。
15.只能輸入非零的正整數:」^\+?[1-9][0-9]*$」。
16.只能輸入非零的負整數:」^\-[1-9][]0-9″*$。
17.只能輸入長度為3的字符:」^.{3}$」。
18.只能輸入由26個英文字母組成的字符串:」^[A-Za-z]+$」。
19.只能輸入由26個大寫英文字母組成的字符串:」^[A-Z]+$」。
20.只能輸入由26個小寫英文字母組成的字符串:」^[a-z]+$」。
21.驗證是否含有^%&’,;=?$\」等字符:」[^%&’,;=?$\x22]+」。
22.只能輸入漢字:」^[\u4e00-\u9fa5]{0,}$」。
23.驗證URL:」^http://([\\w-]+\.)+[\\w-]+(/[\\w-./?%&=]*)?$」。
24.驗證一年的12個月:」^(0?[1-9]|1[0-2])$」正確格式為:」01″~」09″和」10″~」12″。
25.驗證一個月的31天:」^((0?[1-9])|((1|2)[0-9])|30|31)$」正確格式為;」01″~」09″、」10″~」29″和「30」~「31」。
26.獲取日期正則表達式:\\d{4}[年|\-|\.]\\d{\1-\12}[月|\-|\.]\\d{\1-\31}日?
評註:可用來匹配大多數年月日信息。
27.匹配雙字節字符(包括漢字在內):[^\x00-\xff]
評註:可以用來計算字符串的長度(一個雙字節字符長度計2,ASCII字符計1)
28.匹配空白行的正則表達式:\n\s*\r
評註:可以用來刪除空白行
29.匹配HTML標記的正則表達式:<(\S*?)[^>]*>.*?</>|<.*? />
評註:網上流傳的版本太糟糕,上面這個也僅僅能匹配部分,對於複雜的嵌套標記依舊無能為力
30.匹配首尾空白字符的正則表達式:^\s*|\s*$
評註:可以用來刪除行首行尾的空白字符(包括空格、制表符、換頁符等等),非常有用的表達式
31.匹配網址URL的正則表達式:[a-zA-z]+://[^\s]*
評註:網上流傳的版本功能很有限,上面這個基本可以滿足需求
32.匹配帳號是否合法(字母開頭,允許5-16字節,允許字母數字下划線):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
評註:表單驗證時很實用
33.匹配騰訊QQ號:[1-9][0-9]\{4,\}
評註:騰訊QQ號從10 000 開始
34.匹配中國郵政編碼:[1-9]\\d{5}(?!\d)
評註:中國郵政編碼為6位數字
35.匹配ip地址:((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)。


LIND ID Pattern:

@”[a-zA-Z0-9.\\-_]{4,20}”;


Compare function:

// Validate the input string with the given pattern and
// return the result as a boolean
- (BOOL)validateString:(NSString *)string withPattern:(NSString *)pattern
{
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
    
    NSAssert(regex, @"Unable to create regular expression");
    
    NSRange textRange = NSMakeRange(0, string.length);
    NSRange matchRange = [regex rangeOfFirstMatchInString:string options:NSMatchingReportProgress range:textRange];
    
    BOOL didValidate = NO;
    
    // Did we find a matching range
    if (matchRange.location != NSNotFound)
        didValidate = YES;
    
    return didValidate;
}

 

[iOS] Dialog Box

iOS 的輸入框,是使用 UIAlertController。

 

一個簡單的對話框例子

您可以比較一下兩種不同的創建對話框的代碼,創建基礎UIAlertController的代碼和創建UIAlertView的代碼非常相似:

Objective-C版本:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題" message:@"這個是UIAlertController的默認樣式" preferredStyle:UIAlertControllerStyleAlert];

swift版本:

var alertController = UIAlertController(title: "標題", message: "這個是UIAlertController的默認樣式", preferredStyle: UIAlertControllerStyle.Alert)

同創建UIAlertView相比,我們無需指定代理,也無需在初始化過程中指定按鈕。不過要特別注意第三個參數,要確定您選擇的是對話框樣式還是上拉菜單樣式。

 


How to add text input in alertview of ios 8?
http://stackoverflow.com/questions/33996443/how-to-add-text-input-in-alertview-of-ios-8

 

 UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Login"
                                                                                  message: @"Input username and password"
                                                                              preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"name";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"password";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = YES;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * namefield = textfields[0];
        UITextField * passwordfiled = textfields[1];
        NSLog(@"%@:%@",namefield.text,passwordfiled.text);

    }]];
    [self presentViewController:alertController animated:YES completion:nil];

 

 


看要增加幾個選項按鈕都可以,而且都是用Block來做後續處理

範例:

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改暱稱"
 message:@""
 preferredStyle:UIAlertControllerStyleAlert];
 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
 // optionally configure the text field
 textField.keyboardType = UIKeyboardTypeDefault;
 }];
 
 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"送出"
 style:UIAlertActionStyleDefault
 handler:^(UIAlertAction *action) {
 UITextField *textField = [alert.textFields firstObject];
 
 }];
 [alert addAction:okAction];
 
 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
 
 }];
 [alert addAction:cancelAction];
 
 [self presentViewController:alert animated:YES completion:nil];

UIAlertController Example in iOS
http://hayageek.com/uialertcontroller-example-ios/

UIAlertController Changes in iOS 8
https://useyourloaf.com/blog/uialertcontroller-changes-in-ios-8/

 

 

Swift 範例:
https://www.simplifiedios.net/ios-dialog-box-with-input/

 

Apple 官方文件:

UIAlertController
https://developer.apple.com/reference/uikit/uialertcontroller

[iOS] how to change storyboard entry point

Try with following steps.

1) Open StoryBoard.

2) select TabbarControllerwhich you want to set as RootViewController.

3) Open Properties.

4) select InitialView Controller Option.

For help you can see following image.

 

真的很奇怪,為什麼要放在第4個Tab 裡,還放在不明顯的地方…,找半天都找不到。

 

[iOS] 多國語系 (Localization)

首先要有字串檔案,在Resources中新增檔案,iOS->Resource->Strings File,命名為Localizable,不修改這個 file 的名字的話,預設是 File, 所以會開成一個 File.string 檔案,改一下會比較知道這個檔案的用途。

目前我用的是  Xcode v8.3.1

 

進入Xcode專案,首先點專案檔,PROJECT->Info->Localizations新增想要的語言,我是增加了中文,接下來要選擇那些檔案要被多國語言,有UI 的 storyboard  和剛才新增的 String file 可以選,由於UI可以共用,所以只選 String file.

其實,這個晝面弄很久才弄出來,似乎跟 “Use Base Internationalization” 有關,勾掉之後就只剩English, 然後再增加中文就變正常了,新的 Base Internationalization 暫時懶的去研究是什麼新功能。請叫我囫圇吞棗大師~,讀書和做研究都不求甚解。

 

 

String file, 的英文檔:

"About" = "About";

對應到中文的檔案時:

"About" = "關於";

 

在程式碼裡使用 NSLocalizedString(<#key#>, <#comment#>) 來讀取多國化字串。

 

相關文章:

iOS本地化 NSLocalizedString的使用
http://www.jianshu.com/p/3d77c2e76684

NSLocalizedString
https://developer.apple.com/reference/foundation/nslocalizedstring

 

Localizing Your App
https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html