我正在尝试检查用户默认是否存在,如下所示:
func userAlreadyExist() -> Bool {
var userDefaults : NSUserDefaults = NSUserDefaults.standardUserDefaults()
if userDefaults.objectForKey(kUSERID) {
return true
}
return false
}
Run Code Online (Sandbox Code Playgroud)
但是,即使对象不存在,它也总是会返回真实的东西吗?这是检查存在的正确方法吗?
我正在使用shouldChangeCharactersInRange作为使用动态类型搜索的一种方式.
但是我遇到了问题,在文本字段实际更新之前调用了更改角色的内容:
在Objective C中,我使用以下方法解决了这个问题:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
但是,我试过在Swift中写这个:
func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
let txtAfterUpdate:NSString = self.projectSearchTxtFld.text as NSString
txtAfterUpdate.stringByReplacingCharactersInRange(range, withString: string)
self.callMyMethod(txtAfterUpdate)
return true
}
Run Code Online (Sandbox Code Playgroud)
在得到值之前,该方法仍会被调用?
我目前正在我的新故事板中显示一个viewController:
var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"
self.presentViewController(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
但是,这使得viewcontroller没有嵌入式导航控制器.我尝试将"WelcomeID"更改为故事板中的导航控制器 - 但是没有成功.
我在Objective -C中使用了这个,但是不知道如何转换为swift:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SetupStoryboard" bundle:nil];
UINavigationController *navigationController1 = [storyboard instantiateInitialViewController];
navigationController1.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController1.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
WelcomeViewController *vc = (WelcomeViewController *)navigationController1.viewControllers[0];
vc.teststring = @"Hello";
[self presentViewController:navigationController1 animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
你怎么能在swift中做到这一点?
uiviewcontroller uinavigationcontroller ios uistoryboard swift
我想知道如何将下面的客观c代码转换成swift.
这将遍历我想要的视图上的所有子视图,检查它们是否是文本字段,然后检查它们是否为空.
for (UIView *view in contentVw.subviews) {
NSLog(@"%@", view);
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textfield = (UITextField *)view;
if (([textfield.text isEqualToString:""])) {
//show error
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这里是我快速翻译的地方:
for view in self.view.subviews as [UIView] {
if view.isKindOfClass(UITextField) {
//...
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒!
我知道有一种方法可以将子视图带到层次结构的前面.然而,是否有一种方法可以将一个按钮放在一个故事上,并调用一个函数,使该按钮成为顶层,这样它就不会被添加到故事板中的任何其他元素隐藏.
谢谢,
我想知道如何遍历当前可见的所有CollectionView单元格.
在Objective C中,我将实现以下概念:
for(UICollectionView *cell in collectionView.visibleCells){
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试将其更改为swift:
for cell:MyCollectionViewCell in self.collectionView.visibleCells() as cell:MyCollectionViewCell {
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Type 'MyCollectionViewCell' does not conform to protocol 'SequenceType'
Run Code Online (Sandbox Code Playgroud)
如何遍历所有CollectionViewCells
我试图将值传递给新的视图控制器 - 位于新的故事板文件中.但是当我这样做时,我从NewViewController得到的结果总是为零.
下面是我在新故事板中显示视图控制器的方法:
// Show create account page with custom transition
var storyboard : UIStoryboard = UIStoryboard(name: StoryboardName, bundle: nil)
var vc : UIViewController = storyboard.instantiateViewControllerWithIdentifier(NewViewController) as UIViewController
Run Code Online (Sandbox Code Playgroud)
我尝试在这里发送信息:
// Pass the delegate to the first view controller
let newViewController:NewViewController = NewViewController()
newViewController.createAccountDelegate = self
newViewController.teststring = "hello"
Run Code Online (Sandbox Code Playgroud)
然后呈现视图控制器.
vc.transitioningDelegate = self
vc.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
这是我的NewViewController,我尝试接收值.但最终仍然是零.
import UIKit
class NewViewController: UIViewController {
var createAccountDelegate:AccountCreationDelegate!
var teststring:NSString!
override func viewDidLoad() {
super.viewDidLoad()
}
Run Code Online (Sandbox Code Playgroud)
我错误地发送了这些值吗?
我正在尝试使用prepareForSegue方法将字符串传递给下面看到的我的模态视图控制器.见下文:
这是我的初始视图控制器,我将在其中呈现模态视图:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "newProject" {
var newProjectVC:ModalViewController = ModalViewController()
newProjectVC = segue.destinationViewController as ModalViewController
newProjectVC.testString = "hello"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模态视图控制器:
import UIKit
class ModalViewController: UIViewController {
var testString:NSString!
override func viewDidLoad() {
println(self.testString)
}
}
Run Code Online (Sandbox Code Playgroud)
这是故事板中的样子:[在此输入图像描述] [1]
问题是它在这一行引发异常:
newProjectVC = segue.destinationViewController as ModalViewController
Run Code Online (Sandbox Code Playgroud)
我有一种感觉它可能与导航控制器有关但不确定,有什么想法吗?
我试图快速枚举我的所有集合视图单元格,但是下面的这个实现给了我一个警告.
for cell in self.collectionView?.visibleCells() as [UICollectionViewCell] {
// Do Stuff
}
Run Code Online (Sandbox Code Playgroud)
第一行显示以下错误:
postfix的操作数'?' 应该有可选的类型; type是'(UICollectionView,cellForItemAtIndexPath:NSIndexPath) - > UICollectionViewCell'
我已经尝试搞乱选项,并在Xcode 6 Beta 6中使用它,但在"Beta 7"中无济于事
我该如何摆脱这个错误?/写一个循环遍历我的所有CollectionView单元格?
fast-enumeration ios uicollectionview uicollectionviewcell swift
很简单,我希望能够更改标签栏中未选定项目的颜色.
请参阅下面的"观看最多"对象大麦,可以使用默认颜色.
这是我试图实现的代码:
UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIColor.greenColor(), forKey: NSFontAttributeName), forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)

但是,使用此代码不起作用.有谁知道如何在swift中实现这种效果?
ios ×10
swift ×9
objective-c ×2
delegates ×1
for-loop ×1
nsrange ×1
segue ×1
uibutton ×1
uicolor ×1
uistoryboard ×1
uitabbar ×1
uitabbaritem ×1