我正在努力构建一个新的Swift应用程序,大致基于一个旧的Obj-c应用程序.我目前正在为代表们工作
这是我的obj-c代码在.h文件中的样子
@interface MyAppViewController : CustomViewController
@property (nonatomic, weak) id<MyAppViewControllerDelegate> delegate;
@end
@protocol MyAppViewControllerDelegate <NSObject>
- (void)myAppViewController:(MyAppViewController *)controller loggedInStudent: (MYStudent *)student;
- (void)myAppViewControllerWantsSignUp:(MyAppViewController *)controller;
@end
Run Code Online (Sandbox Code Playgroud)
在SWIFT我做了:
class MyAppViewController: CustomViewController {
var delegate: MyAppViewControllerDelegate?
protocol MyAppViewControllerDelegate{
func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent)
func myAppViewControllerWantsSignUp(controller: MyAppViewController)
Run Code Online (Sandbox Code Playgroud)
我已经做了很多阅读和研究,所以我认为我做的基本上是正确的(尽管很快,但是......)
我收到这个错误," Declaration is only valid in file scope" protocol MyAppViewControllerDelegate {
我认为这与在类中声明它有关,所以我把它移出来,只是现在我的代码中的代码不能识别我声明的委托变量..
有任何想法吗?
我可以轻松地在CellForItemAtIndexPath方法中更改单元格的背景颜色
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
cell.backgroundColor = UIColor.blackColor()
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试更改DidSelectItemAtIndexPath中的颜色时,它不起作用.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell: ButtonCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("ButtonCell", forIndexPath: indexPath) as! ButtonCollectionCell {
cell.backgroundColor = UIColor.blackColor()
Run Code Online (Sandbox Code Playgroud)
}
另外我在某处读到使用didSelectItemAtIndexPath不起作用,因为一旦集合视图开始滚动颜色将改变回来
Swift有什么问题?
非常感谢你的帮助
在swift中,我以编程方式声明了textField.
override func viewDidLoad() {
super.viewDidLoad()
textField3.delegate = self
textField3.returnKeyType = UIReturnKeyType.Next;
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
textField3.inputView = datePicker
}
func handleDatePicker(sender: UIDatePicker) {
textField3.text = dateFormatter.stringFromDate(sender.date)
}
Run Code Online (Sandbox Code Playgroud)
我发现所有其他问题与这个与界面构建器的处理相似,并没有帮助.我将如何以编程方式解决此问题?
我有一个应用程序UITextField.当出现包含视图时,我希望键盘向上滑动并开始编辑此文本字段.不确定我会怎么做呢?
我试过了:
func viewDidLoad {
textField.editing == true
textField.selected == true
Run Code Online (Sandbox Code Playgroud) 我正在尝试熟悉do catchswift 2和iOS 9 的新语句
我的问题是,当NSURLSession发生错误时,data参数返回nil,并返回错误.在iOS 8中,这是预期的功能,我们只是使用if语句来确定Data是否为零
然而do catch,有一个新的try关键字,我认为这是为了看看是否有效,如果没有那么默认为任何代码写入catch
但是,由于数据为零,我遇到意外崩溃.这是预期的功能,catch当我的try方法失败时,为什么不被调用?
我正在使用NSURLSession从API中提取数据.
我创建一个dataTaskWith请求,如下所示:
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
print(request)
print(response)
print(error)
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
Run Code Online (Sandbox Code Playgroud)
因为数据崩溃了!是零...因为有一个NSURLSession错误
print(jsonResult)
} catch {
print(error)
}
})
task.resume()
Run Code Online (Sandbox Code Playgroud) 我刚刚更新了Xcode的新测试版,继续使用我的WatchKit应用程序
更新了所有WatchKit扩展文件后,我立即收到错误消息
"初始化程序不会覆盖其超类中指定的初始值设定项"
我在更新之前没有收到此错误,不知道如何修复它.
任何见解?
这是我的代码
class InterfaceController: WKInterfaceController {
override init(context: AnyObject?) {
// Initialize variables here.
super.init(context: context)
// Configure interface objects here.
NSLog("%@ init", self)
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
NSLog("%@ will activate", self)
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
NSLog("%@ did deactivate", self)
super.didDeactivate()
}
}
Run Code Online (Sandbox Code Playgroud) 我正在努力将我的应用程序转换为swift,但我遇到了一个我还不太了解的快速错误.
在Obj-c中我有一个for in循环:
for (MYVisitor *visitor in visitors) {
if (visitor.boxState == MYVisitorBoxStateChatting) {
[self.chattingVisitors addObject:visitor];
}
else {
[self.unknownVisitors addObject:visitor];
}
}
Run Code Online (Sandbox Code Playgroud)
我很快就有:
for visitor: MYVisitor in visitors {
if visitor.boxState == MYVisitorBoxStateChatting {
self.chattingVisitors.addObject(visitor)
}
else {
self.unknownVisitors.addObject(visitor)
}
}
Run Code Online (Sandbox Code Playgroud)
我在swift中遇到错误,说: NSArray cannot be implicitly downcast to MYVisitor, did you mean to use as to force downcast?
Xcode建议这个改变:for visitor: FHVisitor in visitors as MYVisitor {...
然后给我这个错误:MYVisitor does not conform to protocol SequenceType
我有一个UITableViewController约五个不同的静态细胞.在其中一个单元格中,我试图加载动态UITableView
在界面构建器中,我标记了主UITableViewController tableView0,然后我标记了动态tableView1.
每次我尝试加载控制器时都会崩溃.这是我到目前为止的粗略代码:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
switch tableView.tag {
case 0:
return 2;
case 1:
return 1;
default:
break;
}
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch tableView.tag {
case 0:
return 5;
case 1:
return 2;
default:
break;
}
return 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ComboInfoCell", forIndexPath: indexPath) as! UITableViewCell …Run Code Online (Sandbox Code Playgroud) swift ×8
ios ×7
objective-c ×2
uitextfield ×2
ios9 ×1
nsurlsession ×1
swift2 ×1
uidatepicker ×1
uitableview ×1
watchkit ×1