小编Lav*_*der的帖子

在查看弹出窗口之前存储UITextField内容

我确信这是在Apple文档中,或者必须在这个论坛的某个地方得到解答,因为它看起来很基本,但我自己也找不到它,也不是一个特别优雅的解决方案.

我所拥有的是一个UIViewController,它在其导航堆栈上推送编辑视图.编辑视图中包含一堆UITextField.如果其中一人正在编辑时返回按钮被触摸时,原始视图的viewWillAppear中方法任一的UITextField委托方法之前调用textFieldShouldEndEditingtextFieldDidEndEditing,或IB链接动作textFieldEditingEnded方法被调用.

以下是一些代码,我希望这些代码能够更加清晰:

在UIViewController中:

- (void) viewWillAppear: (BOOL) animated {
    [super viewWillAppear: animated];
    NSLog( @"Entering view will appear for master view" );
    nameLabelField.text = objectToEdit.name;
}
- (IBAction) editMyObject: (id) sender {
    NSLog( @"Editing the object" );
    EditViewController *evc = [[EditViewController alloc] initWithNibName: @"EditTableView" bundle: nil];
    evc.editedObject = objectToEdit;
    [self.navigationController pushViewController: evc animated: YES];
    [evc release];
}
Run Code Online (Sandbox Code Playgroud)

在EditViewController <UITextFieldDelegate>中:

- (void) viewWillAppear: (BOOL) animated {
    [super viewWillAppear: animated];
    nameField.text = editedObject.name;
}
- (void) …
Run Code Online (Sandbox Code Playgroud)

iphone uitextfield popviewcontroller

13
推荐指数
1
解决办法
2926
查看次数

如何在NSNumberFormatter的输出上强制使用符号字符

我想使用数字格式化程序来生成我的输出,因此数字会自动格式化为用户的语言环境,但是我希望它在printf()中"%+.1f"一样工作,它总是有指定的符号.

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterDecimalStyle;
nf.maximumFractionDigits = 1;

double val = 3.1234;
label.text = [NSString stringWithFormat: @"XXX %@ XXX", [nf stringFromNumber: [NSNumber numberWithDouble: val]]];
Run Code Online (Sandbox Code Playgroud)

我希望标签在美国出现"XXX +3.1 XXX",并为任何其他位置提供相应但等效的字符串.我能找到的唯一的东西是setPositiveFormat:setPositivePrefix:.

但我希望设置的格式,因为我不知道如何在其他国家的格式化数字; 我不知道是否使用加号来指定阿拉伯语或俄语中的正数或某些我没有想到的文化.我知道,例如,小数点,逗号,空格等在欧洲国家与美国相比都有不同的含义 - 对于+/-符号也是如此吗?

我现在做的是:

label.text = [NSString stringWithFormat: @"XXX %s%@ XXX", (val < 0) ? "" : "+",
    [nf stringFromNumber: [NSNumber numberWithDouble: val]]];
Run Code Online (Sandbox Code Playgroud)

但这假定'+'和' - '对所有格式都是正确的.

我敢肯定它一定存在,因为它是一个标准格式化的东西,自从黑暗时代以来一直在printf()中 ...

objective-c nsnumberformatter ios

11
推荐指数
2
解决办法
5222
查看次数

这是NSIndexSet中的一个错误enumerateIndexesUsingBlock吗?

在我的单元测试中,我正在查看一些边界条件,我的测试仍然失败.当它的索引一直扩展到NSNotFound-1(每个文档的最大合法值)时,我将其追溯到枚举索引集.

使用此测试代码:

// Create an index set with NSNotFound-5, NSNotFound-4, NSNotFound-3, 
// NSNotFound-2 and NSNotFound-1
NSIndexSet *testSet = [NSIndexSet indexSetWithIndexesInRange:
    NSMakeRange( NSNotFound - 5, 5 )];
NSLog( @"Index set with %lu entries: %@", (unsigned long) testSet.count, testSet );
NSLog( @"NSNotFound is %lu and NSNotFound-1 is %lu", NSNotFound, NSNotFound - 1 );
__block int count = 0;
[testSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
    NSLog( @"- %lu", (unsigned long) idx );
    ++count;
}];
NSLog( @"The count is %d", count );
Run Code Online (Sandbox Code Playgroud)

在日志输出中我得到:

Index …
Run Code Online (Sandbox Code Playgroud)

macos cocoa nsindexset ios

6
推荐指数
1
解决办法
1037
查看次数

如何使用UISearchDisplayController预加载或初始化搜索?

我在InterfaceBuilder中的UITableViewController中添加了一个UISearchDisplayController.通过添加searchBar: searchBar textDidChange: searchText为表和调用重新生成数据数组的方法,可以很容易地使其工作reloadData.

但是我想在下次用户触摸搜索栏时显示上一个搜索节目,因为触摸(X)并清除它比再次输入更容易.但我无法做到这一点.以下是我尝试过的代码:

- (void) searchBar: (UISearchBar *) searchBar textDidChange: (NSString *) searchText {
    if( searchText && [searchText length] > 0 ) {
        self.displayedRows = [[NSMutableArray initWithCapacity: 1];
        for( NSString *l in self.allRows ) {
            NSRange r = [l rangeOfString: searchText];
            if( r.location != NSNotFound )
                [self.displayedRows addObject: l];
        }
        [self.searchDisplayController.searchResultsTableView reloadData];
    }
}

- (void) searchDisplayControllerWillBeginSearch: (UISearchDisplayController *) controller {
    if( self.lastSearch && [self.lastSearch length] > 0 ) {
        controller.searchBar.text = self.lastSearch;
        [controller.searchResultsTableView …
Run Code Online (Sandbox Code Playgroud)

iphone initialization objective-c uisearchdisplaycontroller

5
推荐指数
1
解决办法
2377
查看次数

在UITableViewController中弹出多个级别

我希望能够从UITableViewController堆栈弹出多个视图.例如,在Apple DrillDownSave示例中,当查看级别3以返回级别1或查看项目时,按下按钮时返回级别2.

我试过了:

[self.navigationController.parentViewController.navigationController popViewControllerAnimated: NO];
[self.navigationController popViewControllerAnimated: NO];  
Run Code Online (Sandbox Code Playgroud)

[self.navigationController popViewControllerAnimated: NO];  
[self.navigationController.parentViewController.navigationController popViewControllerAnimated: NO];
Run Code Online (Sandbox Code Playgroud)

但是这些只留下一个popViewControllerAnimated:是否有捷径可寻?

iphone objective-c

2
推荐指数
1
解决办法
1387
查看次数