小编Ork*_*ade的帖子

在Swift中将Dictionary转换为JSON

我创建了下一个词典:

var postJSON = [ids[0]:answersArray[0], ids[1]:answersArray[1], ids[2]:answersArray[2]] as Dictionary
Run Code Online (Sandbox Code Playgroud)

我得到:

[2: B, 1: A, 3: C]
Run Code Online (Sandbox Code Playgroud)

那么,我怎样才能将它转换为JSON?

serialization json swift

169
推荐指数
10
解决办法
21万
查看次数

如何在tableView加载时显示活动指示器?

当我在我的标签之间切换它加载几秒钟,我想知道我的数据正在加载.为此,我决定添加一个活动指标.

我写了一个小函数:

func showActivityIndicator() {
    dispatch_async(dispatch_get_main_queue()) {
        self.spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
        self.spinner.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0)
        self.spinner.center = CGPoint(x:self.loadingView.bounds.size.width / 2, y:self.loadingView.bounds.size.height / 2)

        self.loadingView.addSubview(self.spinner)
        self.view.addSubview(self.loadingView)
        self.spinner.startAnimating()
    }
}
Run Code Online (Sandbox Code Playgroud)

这将显示我的指标.当我从infoController中点击按钮时尝试使用它:

@IBAction func goToMainFromInfo(sender: AnyObject) {
        self.showActivityIndicator()
        self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil)
        self.hideActivityIndicator()
    }
}
Run Code Online (Sandbox Code Playgroud)

我在segue执行之前展示并隐藏在之后.它对我没有帮助.当我尝试使用同步时:

@IBAction func goToMainFromInfo(sender: AnyObject) {
    dispatch_async(dispatch_get_main_queue()) {
        self.showActivityIndicator()
        self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil)
        self.hideActivityIndicator()
    }
}
Run Code Online (Sandbox Code Playgroud)

但它也没有帮助.当我按Tab键时,不透明度变为0.5,我在加载时等待.但我没有看到我的活动指标.

问题是什么?

uitableview uiactivityindicatorview ios swift

32
推荐指数
4
解决办法
5万
查看次数

如何在Swift中获取图像文件大小?

我在用

UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate
Run Code Online (Sandbox Code Playgroud)

这些代表从我的画廊或我的相机中选择图像.那么,如何在选择图像后获得图像文件大小?

我想用这个:

let filePath = "your path here"
    var fileSize : UInt64 = 0

    do {
        let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath)

        if let _attr = attr {
            fileSize = _attr.fileSize();
            print(fileSize)
        }
    } catch {
    }
Run Code Online (Sandbox Code Playgroud)

但是在这里我需要一条路径,但是如果没有路径,我只能通过图像文件获得?

uiimageview uiimagepickercontroller ios swift

29
推荐指数
3
解决办法
4万
查看次数

在Swift中的AppDelegate中显示警报

我尝试下一个代码片段:

var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

在我的AppDelegate中,但它在控制台中打印出下一个错误:

Warning: Attempt to present <UIAlertController: 0x7ff6cd827a30> on <Messenger.WelcomeController: 0x7ff6cb51c940> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

ios swift uialertcontroller

24
推荐指数
6
解决办法
3万
查看次数

无法更改搜索栏背景颜色

我有一个搜索栏:

let searchBar:UISearchBar = UISearchBar(frame: CGRectMake((searchView.frame.width - UIScreen.mainScreen().bounds.width / 1.6) / 2, 0, UIScreen.mainScreen().bounds.width / 1.6, 24))
Run Code Online (Sandbox Code Playgroud)

我想改变文字输入部分的背景颜色.为此,我尝试过:

searchBar.barTintColor = UIColor(red: 0/255, green: 74/255, blue: 103/255, alpha: 1)

searchBar.backgroundColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)

但这两种变体都不起作用.如何更改我的UISearchBar textInput部分的背景颜色以及我做错了什么?

uisearchbar ios swift

19
推荐指数
13
解决办法
3万
查看次数

更新Swift中的徽章计数器

使用以下代码,我会在应用程序编译后立即在徽章图标中获取(2):

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.badge = 2
    installation.saveInBackground()
}
Run Code Online (Sandbox Code Playgroud)

我确实尝试了下一个变体:初始化一个新的var badgeCount = 0和后来的:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    badgeCount++
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation.badge = badgeCount
    installation.saveInBackground()
}
Run Code Online (Sandbox Code Playgroud)

但是当我收到新通知时,它不会更新为+1.有谁知道如何解决它?

push-notification badge ios parse-platform swift

16
推荐指数
4
解决办法
3万
查看次数

如何在UITableView删除按钮上添加图像?

我有一个UITableView,我已经添加了编辑操作.现在我想在删除按钮标签上方添加图像,如下所示:

在此输入图像描述

这是我的代码:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let blockAction = UITableViewRowAction(style: .Normal, title: "Block") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
        //TODO: Delete the row at indexPath here
    }
    blockAction.backgroundColor = UIColor.redColor()

    return [blockAction]
}
Run Code Online (Sandbox Code Playgroud)

如何在Delete按钮上添加图像?

uitableview ios swift

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

在Swift中以编程方式创建webView

我想在我的ViewController中创建类似图像库的东西.为此,我需要多个webView,具体取决于我从JSON请求获得的图像数量.如果需要,我如何插入新的webView?

在此输入图像描述

正如您在上图中看到的,我在ViewController中有一个scrollView和一个UIWebView.如有必要,我如何在第一个(第二个,第三个等)内创建一个新的webView?可能吗?

uiwebview webview ios swift

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

你如何确认一个字符串只包含Swift中的数字?

如果searchView只包含数字,我该如何检查?

我找到了这个:

if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
Run Code Online (Sandbox Code Playgroud)

但它检查文本是否包含任何数字.如果所有文本只包含Swift中的数字,我该怎么办?

regex ios swift

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

如何全局更改UINavigationBar颜色?

我想从AppDelegate全局更改整个应用程序的UINavigationBar颜色.我这样做:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
UINavigationBar.appearance().tintColor = UIColor(red: 63, green: 172, blue: 236, alpha: 1)
}
Run Code Online (Sandbox Code Playgroud)

但是,我不知道为什么,它不会改变导航栏的颜色.

我把导航栏连接成了 Editor > Embed In > Navigation Controller

如何设置NavBar的颜色?

uinavigationcontroller ios swift

9
推荐指数
1
解决办法
9108
查看次数