Ios 点击按钮后额外零
Xcode 7 beta 6.Ios 点击按钮后额外零,ios,iphone,swift,Ios,Iphone,Swift,Xcode 7 beta 6. 我一次点击save按钮就显示了两次nils? 我应该从哪里开始故障排除? 我有两个ViewController。首先是ProfileTableViewController。第二个是addUIViewController 应用程序流程为 1.在选择场景上的制表符加号。 2.添加姓名、年龄、专业、描述。然后保存。 3.展开序列发回新的配置文件对象。 4.TableViewController更新场景中的记录。 现在我很困惑。我的解卷序列的实现是否错误? Profile
我一次点击
save
按钮就显示了两次nil
s?我应该从哪里开始故障排除?
我有两个ViewController。首先是
ProfileTableViewController
。第二个是addUIViewController
应用程序流程为
1.在
选择场景上的制表符加号。
2.添加姓名、年龄、专业、描述。然后保存。
3.展开序列发回新的配置文件对象。
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;
}
};
}
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);
}
}
}
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;
}
}
}
addler.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;
}
};
}
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);
}
}
}
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;
}
}
}
原因:按住ctrl键并拖动保存
按钮以退出
解决方案:通过从标题
拖动到退出
原因:按住ctrl键并拖动保存
按钮到退出
解决方案:通过从标题
拖动到退出
删除并重新创建序列,您知道“nil
”的来源吗?如果没有,您可以在对“print
”的调用中添加一点额外的信息。例如:“打印(“addNameTextField是\(addNameTextField.text)”)
”。这样,您就可以很容易地分离出哪个“print
”正在打印零。@MichaelDautermann Name->Optional(“txt”)、Age->Optional(“22”)、speciality->Optional(“you”)、Description->descriptionDDD。我会把我的print
行写得更详细一些。你知道“nil
”是从哪里来的吗?如果没有,您可以在对“print
”的调用中添加一点额外的信息。例如:“打印(“addNameTextField是\(addNameTextField.text)”)
”。这样,您就可以很容易地分离出哪个“print
”正在打印零。@MichaelDautermann Name->Optional(“txt”)、Age->Optional(“22”)、speciality->Optional(“you”)、Description->descriptionDDD。我将把我的打印
行放在信息量更大的位置。