如何引用斯威夫特游乐场本身?

时间:2021-11-28 21:43:49

from How to create a button programmatically?

如何以编程方式创建一个按钮?

self doesn't work in swift playground :

self在swift的游乐场里不工作:

button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

error :

错误:

Playground execution failed: error: :42:13: error: use of unresolved identifier 'self'

操场执行失败:错误:::42:13:错误:使用未解决的标识符“self”

I also tried to create a class with buttonAction method inside it compiles but when I click on the button nothing prints in console

我还尝试创建一个类,其中包含buttonAction方法,它会编译,但是当我点击按钮时,控制台里没有任何东西会打印出来

import UIKit

class myself {


    func buttonAction(sender:UIButton!)
    {
        println("Button tapped")
    }

}

var s = myself()

// Create View
var f = CGRect(x:0,y:0,width:200,height:200)
var view = UIView(frame:f)


// Create Button
var b = UIButton(frame: f)
b.setTitle("Hello", forState: UIControlState.Normal)
b.addTarget(s, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

view.addSubview(b)

view

3 个解决方案

#1


7  

Unfortunately, controls in playgrounds are not interactive.

不幸的是,操场上的控制不是交互式的。

I tried getting around the non-interactivity of playground controls the other day by creating and displaying an NSWindow for a Mac playground, however, the result was that the window simply flashed behind the playground.

前些日子,我试图通过为Mac游乐场创建和显示一个NSWindow来绕过游乐场控件的非交互性,但结果是,窗口只是在游乐场后面闪现。

Best that I can tell after some probing into NSProcess, NSBundle, NSApplication and watching the system Activity Monitor, the playground actually compiles your code, loads it into a stub app, executes, the playground captures the results and displays them then the stub app exits or waits around for the next execution. This means by the time you see the results, the code is no longer executing.

最好的我可以告诉一些探讨NSProcess之后,NSBundle,NSApplication看系统活动监视器,操场上实际上编译你的代码,将它加载到一个存根应用程序,执行时,操场上捕获的结果并将它们显示然后存根程序退出或在等待下一个执行。这意味着当您看到结果时,代码就不再执行了。

If you're curious, you can try the code below in a playground to see what I mean. If you open Activity Monitor and filter on PlaygroundStub_OSX you can see the process that runs the code launch. It is oftentimes in a "Not responding" state. I've also included a screen capture of the results of the probing portion of the code.

如果你好奇,你可以在操场上试试下面的代码,看看我是什么意思。如果在PlaygroundStub_OSX上打开活动监视器和过滤器,您可以看到运行代码启动的过程。它通常处于“没有反应”的状态。我还包含了代码探测部分结果的屏幕截图。

import Cocoa

var app = NSApplication.sharedApplication()
app.hidden

var procInfo = NSProcessInfo.processInfo()
procInfo.processName
procInfo.arguments

var bundle = NSBundle.mainBundle()
bundle.bundlePath



var window = NSWindow(contentRect: NSRect(x: 30, y: 30, width: 400, height: 400), styleMask: NSTitledWindowMask, backing: .Buffered, defer: false)


var view = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 300))

var textField = NSTextField(frame: NSRect(x: 30, y: 30, width: 100, height: 20))

textField.stringValue = "Test"

view.addSubview(textField)

var button = NSButton(frame: NSRect(x: 30, y: 60, width: 100, height: 30))

button.highlight(true)

var buttonCell:NSButtonCell = button.cell() as NSButtonCell
buttonCell.bezelStyle = NSBezelStyle.RoundedBezelStyle


view.addSubview(button)

window.contentView.addSubview(view)



window.makeKeyAndOrderFront(nil)

如何引用斯威夫特游乐场本身?

UPDATE

更新

I was able to get a semi-functional OS X window in a Swift playground and placed the code in a github repo. I'm not sure if an interactive UIKit based view is possible yet.

我能够在一个快速的游乐场中获得一个半功能性的OS X窗口,并将代码放在github的repo中。我不确定基于交互UIKit的视图是否可行。

#2


7  

With Xcode 7.3 this is now supported. You'll need to declare an ObjectiveC object or expose a function using @objc keyword as a target:

现在支持Xcode 7.3。您需要声明一个ObjectiveC对象或使用@objc关键字公开一个函数作为目标:

class Responder : NSObject {
    func action() {
        print("Button pressed!")
    }
}

let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))
XCPlaygroundPage.currentPage.liveView = containerView
let responder = Responder()

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
button.backgroundColor = UIColor.greenColor()
button.setTitle("TEST", forState: .Normal)
button.addTarget(responder, action: #selector(Responder.action), forControlEvents: .TouchUpInside)
containerView.addSubview(button)

#3


1  

I do this successfully with Cocoa.

我用可可成功地做到了这一点。

Make your class a subclass of NSObject. Create an NSWindow in your class and move the button and the action into your class. Then you can set the button's target to self.

使类成为NSObject的子类。在您的类中创建一个NSWindow,并将按钮和操作移动到您的类中。然后可以将按钮的目标设置为self。

Try it with UIKit and let us know if it works.

用UIKit试试,让我们知道它是否有效。

#1


7  

Unfortunately, controls in playgrounds are not interactive.

不幸的是,操场上的控制不是交互式的。

I tried getting around the non-interactivity of playground controls the other day by creating and displaying an NSWindow for a Mac playground, however, the result was that the window simply flashed behind the playground.

前些日子,我试图通过为Mac游乐场创建和显示一个NSWindow来绕过游乐场控件的非交互性,但结果是,窗口只是在游乐场后面闪现。

Best that I can tell after some probing into NSProcess, NSBundle, NSApplication and watching the system Activity Monitor, the playground actually compiles your code, loads it into a stub app, executes, the playground captures the results and displays them then the stub app exits or waits around for the next execution. This means by the time you see the results, the code is no longer executing.

最好的我可以告诉一些探讨NSProcess之后,NSBundle,NSApplication看系统活动监视器,操场上实际上编译你的代码,将它加载到一个存根应用程序,执行时,操场上捕获的结果并将它们显示然后存根程序退出或在等待下一个执行。这意味着当您看到结果时,代码就不再执行了。

If you're curious, you can try the code below in a playground to see what I mean. If you open Activity Monitor and filter on PlaygroundStub_OSX you can see the process that runs the code launch. It is oftentimes in a "Not responding" state. I've also included a screen capture of the results of the probing portion of the code.

如果你好奇,你可以在操场上试试下面的代码,看看我是什么意思。如果在PlaygroundStub_OSX上打开活动监视器和过滤器,您可以看到运行代码启动的过程。它通常处于“没有反应”的状态。我还包含了代码探测部分结果的屏幕截图。

import Cocoa

var app = NSApplication.sharedApplication()
app.hidden

var procInfo = NSProcessInfo.processInfo()
procInfo.processName
procInfo.arguments

var bundle = NSBundle.mainBundle()
bundle.bundlePath



var window = NSWindow(contentRect: NSRect(x: 30, y: 30, width: 400, height: 400), styleMask: NSTitledWindowMask, backing: .Buffered, defer: false)


var view = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 300))

var textField = NSTextField(frame: NSRect(x: 30, y: 30, width: 100, height: 20))

textField.stringValue = "Test"

view.addSubview(textField)

var button = NSButton(frame: NSRect(x: 30, y: 60, width: 100, height: 30))

button.highlight(true)

var buttonCell:NSButtonCell = button.cell() as NSButtonCell
buttonCell.bezelStyle = NSBezelStyle.RoundedBezelStyle


view.addSubview(button)

window.contentView.addSubview(view)



window.makeKeyAndOrderFront(nil)

如何引用斯威夫特游乐场本身?

UPDATE

更新

I was able to get a semi-functional OS X window in a Swift playground and placed the code in a github repo. I'm not sure if an interactive UIKit based view is possible yet.

我能够在一个快速的游乐场中获得一个半功能性的OS X窗口,并将代码放在github的repo中。我不确定基于交互UIKit的视图是否可行。

#2


7  

With Xcode 7.3 this is now supported. You'll need to declare an ObjectiveC object or expose a function using @objc keyword as a target:

现在支持Xcode 7.3。您需要声明一个ObjectiveC对象或使用@objc关键字公开一个函数作为目标:

class Responder : NSObject {
    func action() {
        print("Button pressed!")
    }
}

let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))
XCPlaygroundPage.currentPage.liveView = containerView
let responder = Responder()

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
button.backgroundColor = UIColor.greenColor()
button.setTitle("TEST", forState: .Normal)
button.addTarget(responder, action: #selector(Responder.action), forControlEvents: .TouchUpInside)
containerView.addSubview(button)

#3


1  

I do this successfully with Cocoa.

我用可可成功地做到了这一点。

Make your class a subclass of NSObject. Create an NSWindow in your class and move the button and the action into your class. Then you can set the button's target to self.

使类成为NSObject的子类。在您的类中创建一个NSWindow,并将按钮和操作移动到您的类中。然后可以将按钮的目标设置为self。

Try it with UIKit and let us know if it works.

用UIKit试试,让我们知道它是否有效。