为什么self
在Objective-C中使用静态上下文?
我认为这是允许的,然后我遇到了内存错误,花了我一个星期才发现这self
不是从类调用其他静态方法而不是键入类名的别名.
Xcode及其编译器似乎非常聪明地找到常见的陷阱,为什么它甚至不会产生类似的警告呢?
我正在使用Facebook SDK开发一个ios应用程序来登录.我LogInViewController
在故事板中设置了一个初始视图控制器,用户使用FB帐户登录.
我有另一个ViewController,一旦用户登录就可以正确加载.
在我正在检查的AppDelegate文件中currentAccessToken
,如果它不是nil,我将直接加载第二个ViewController,因为用户已经登录.
但是,currentAccessToken
如果我退出应用程序并重新启动它,则总是为零.它只有在我按下主页按钮并重新打开应用程序时它才有效,因为它仍然在后台运行.
以下是代码中的详细信息:
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.customNavigationBar()
if (!isIcloudAvailable()) {
self.displayAlertWithTitle("iCloud", message: "iCloud is not available." +
" Please sign into your iCloud account and restart this app")
return true
}
if (FBSDKAccessToken.currentAccessToken() != nil) {
self.instantiateViewController("MapViewController", storyboardIdentifier: "Main")
}
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
application,
openURL: url, …
Run Code Online (Sandbox Code Playgroud) 我已经很久没学过iOS或Swift了.使用最新的Xcode更新之一,我在计算机上制作的许多应用程序现在似乎都使用了过时的语法.
Xcode通过将它转换为新语法来谈论我们,但通常没有解决任何问题,我有一个新问题.这是我转换语法后第一个应用程序之一的代码.我收到一个错误说:
可选类型'String?'的值 没有打开; 你的意思是用'!' 要么 '?' ?
我知道这一定很简单,但我不知道如何解决它.这是我的代码:
@IBAction func findAge(sender: AnyObject) {
var enteredAge = Int(age.text)
if enteredAge != nil {
var catYears = enteredAge! * 7
resultLabel.text = "Your cat is \(catYears) in cat years"
} else {
resultLabel.text = "Please enter a whole number"
}
}
Run Code Online (Sandbox Code Playgroud) 我想在这里做一些非常基本的事情.我正在使用基于块的UIView动画将当前离开屏幕的子视图带到当前视图的中心.显然这sp.view.frame = self.view.center
条线是问题所在.最后,我该怎么做我想要的?
[UIView animateWithDuration:1 delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
sp.view.frame = self.view.center;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
调整 textView 的大小后,我希望 textContainer 与 textView 的大小相同(我希望文本与调整大小的 textView 的宽度相同)。所以我现在正在做的是我以UITextView
编程方式创建一个,然后(在 textView 中有或没有文本)我通过更改它的边界来调整 textView 的大小,但 textContainer 保持相同的大小(文本位于比 textView 更小的矩形中,看起来很糟糕)。
这是一些代码:
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer()
layoutManager.addTextContainer(textContainer)
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
self.textView = UITextView(frame: textViewFrame, textContainer: textContainer)
Run Code Online (Sandbox Code Playgroud)
然后:
self.textView.bounds = CGRect(x, y, newWidth, newHeight)
Run Code Online (Sandbox Code Playgroud)
目前我不知道为什么它不起作用并且找不到解决方案。
在iOS 8.4中以编程方式加载UIViewController时,我一直看到IBOutlets注册为nil.这会导致任何iOS 8.4设备崩溃.完全相同的代码在iOS 9和9.0.1上顺利运行.作为参考,这是视图控制器的片段
class B8AVPermissionsViewController: B8BaseViewController {
@IBOutlet weak var closeButton: UIButton!
@IBOutlet weak var cameraButton: UIButton!
@IBOutlet weak var microphoneButton: UIButton!
var delegate: B8PermissionRequestingDelegate? = nil;
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated);
NSLog("cameraButton: \(cameraButton)")
}
Run Code Online (Sandbox Code Playgroud)
打印出来 cameraButton: nil
创建它的代码如下所示:
self.permissionViewController = B8AVPermissionsViewController()
self.permissionViewController!.delegate = self
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(self.permissionViewController!, animated: true, completion: nil)
})
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
ios ×4
swift ×4
animation ×1
fbsdk ×1
ios8 ×1
iphone ×1
objective-c ×1
uitextview ×1
unwrap ×1
xcode ×1