Xcode 7 beta 6.
我单击save
按钮会显示2次带有nil
s的段号?
我应该在哪里开始排查?
2015-09-03 11:58:38.804 demoProject[27331:856955] Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 1336863583_PortraitChoco_iPhone-Simple-Pad_Default
sending profileNew object to TableViewController
2015-09-03 11:58:46.238 demoProject[27331:856955] I'm back from other controller!
nil
Save btn clicked addUIViewController
Name : Optional("txt")
Age : Optional("22")
Specialty : Optional("you")
Description : Desciptionddd
sending profileNew object to TableViewController
2015-09-03 11:58:46.240 demoProject[27331:856955] I'm back from other controller!
nil
我有2个ViewController。首先是ProfileTableViewController
。第二个是addUIViewController
申请流程是
1. Select
场景上的标签加号
2.添加姓名,年龄,专业,描述。然后保存。
3.展开segue发回新的配置文件对象
4. TableViewController更新场景中的记录
现在我很困惑。我实施的解除偏见是错误的吗?
Profile.swift
import UIKit
class Profile {
var name : String;
var age : Int;
var specialty : String?;
var description : String?;
init?(name : String, age: Int, specialty : String?, description : String?){
self.name = name;
self.age = age;
self.specialty = specialty;
self.description = description;
if (name.isEmpty || age < 0){
return nil;
}
};
}
ProfileTableViewController.swift
import UIKit
class ProfileTableViewController: UITableViewController {
var profiles = [Profile]();
var profileNew : Profile?;
override func viewDidLoad() {
super.viewDidLoad()
func loadSampleProfiles(){
let profile1 = Profile(name: "Ant", age: 18, specialty: "Runner", description: "Ants are eusocial insects of the family Formicidae /fɔrˈmɪsɨdiː/ and, along with the related wasps and bees EOF");
let profile2 = Profile(name: "Bee", age: 11, specialty: "Sky Diver", description: "Bees are flying insects closely related to wasps and ants, known for their role in pollination EOF");
let profile3 = Profile(name: "Cat", age: 14, specialty: "Stalker", description: "The domestic cat[1][2] (Felis catus EOF");
let profile4 = Profile(name: "Dog", age: 19, specialty: "Proxy", description: "In engineering a dog is a tool that prevents movement or imparts movement by offering physical obstructioEOF");
let profile5 = Profile(name: "Earth", age: 20, specialty: "Supplier", description: "Earth, also called the world[n 5] (and, less frequently, Gaia[n 6] or, in Latin, Terra[26]), is the thirEOF");
profiles += [profile1!, profile2!, profile3!, profile4!, profile5!];
};
loadSampleProfiles();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profiles.count;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ProfileTableViewCell";
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ProfileTableViewCell
let profile = profiles[indexPath.row];
cell.nameLabel.text = profile.name;
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true);
let row = indexPath.row;
print("Row:\(row)");
print(profiles[row].name , profiles[row].age);
performSegueWithIdentifier("segueTest", sender: row);
}
// Mark: Actions
@IBAction func backFromOtherController(segue: UIStoryboardSegue) {
NSLog("I'm back from other controller!")
print(profileNew?.toString());
//add the new profile
if(profileNew != nil){
profiles += [profileNew!];
//update the tableview
let indexPath = NSIndexPath(forRow: profiles.count, inSection: 0);
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
print(indexPath);
}
}
}
addUIController.swift
import UIKit
class addUIViewController: UIViewController {
// Mark: Properties
@IBOutlet weak var addNameTextField: UITextField!
@IBOutlet weak var addAgeTextField: UITextField!
@IBOutlet weak var addSpecialtyTextField: UITextField!
@IBOutlet weak var descUIViewController: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
}
// Mark: Actions
var profileNew : Profile?;
@IBOutlet weak var saveButton: UIButton!
@IBAction func saveHit(sender: AnyObject) {
print("Save btn clicked addUIViewController");
print("Name : " + addNameTextField.text);
print("Age : " + addAgeTextField.text);
print("Specialty : " + addSpecialtyTextField.text);
print("Description : " + descUIViewController.text);
if(addNameTextField.text == nil || addAgeTextField.text == nil || addSpecialtyTextField.text == nil || descUIViewController.text == nil){
self.profileNew = nil;
print("Got nil");
}
let profileNew = Profile(name : addNameTextField.text!, age : Int(addAgeTextField.text!)!, specialty : addSpecialtyTextField.text!, description : descUIViewController.text! );
self.performSegueWithIdentifier("profileNew", sender: profileNew)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "profileNew"){
let viewControllerReceiver = segue.destinationViewController as? ProfileTableViewController;
print("sending profileNew object to TableViewController");
viewControllerReceiver?.profileNew = profileNew;
}
}
}
答案 0 :(得分:0)
原因:ctrl拖动save
按钮至Exit
解决方案:从Title
拖至Exit
删除并重新创建segue。