前提・実現したいこと
NextViewControllerにUILabelを追加するとエラーが出てしまいます。部品数が少ない時にはきちんと表示できていたので何故エラーが出てしまうのかとその解決法がわかりません。教えていただけると幸いです。
追加するとエラーが出てしまう部分はコメントアウトにしています。
発生している問題・エラーメッセージ
Thread 1: signal SIGABRT 2020-06-23 01:25:08.011779+0900 timeschedle1[22433:1508678] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview' *** First throw call stack: ( 0 CoreFoundation 0x00000001070fb6fb __exceptionPreprocess + 331 1 libobjc.A.dylib 0x0000000104ef7ac5 objc_exception_throw + 48 2 CoreFoundation 0x00000001070fb555 +[NSException raise:format:] + 197 3 UIKitCore 0x000000010b224f17 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 118 4 timeschedle1 0x00000001045ff091 $s12timeschedle118NextViewControllerC11viewDidLoadyyF + 7441 5 timeschedle1 0x00000001046001f4 $s12timeschedle118NextViewControllerC11viewDidLoadyyFTo + 36 6 UIKitCore 0x000000010a75b43b -[UIViewController loadViewIfRequired] + 1183 7 UIKitCore 0x000000010a75b868 -[UIViewController view] + 27 8 UIKitCore 0x000000010a6830fc -[_UIFullscreenPresentationController _setPresentedViewController:] + 89 9 UIKitCore 0x000000010a6771f2 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133 10 UIKitCore 0x000000010a76e4ce -[UIViewController _presentViewController:withAnimationController:completion:] + 3700 11 UIKitCore 0x000000010a77111b __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 98 12 UIKitCore 0x000000010a771633 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 511 13 UIKitCore 0x000000010a771082 -[UIViewController _presentViewController:animated:completion:] + 172 14 UIKitCore 0x000000010a7712e0 -[UIViewController presentViewController:animated:completion:] + 150 15 timeschedle1 0x0000000104606493 $s12timeschedle114ViewControllerC13monday1Button6senderySo8UIButtonC_tF + 499 16 timeschedle1 0x00000001046064fc $s12timeschedle114ViewControllerC13monday1Button6senderySo8UIButtonC_tFTo + 60 17 UIKitCore 0x000000010ad5f204 -[UIApplication sendAction:to:from:forEvent:] + 83 18 UIKitCore 0x000000010a7b4c19 -[UIControl sendAction:to:forEvent:] + 67 19 UIKitCore 0x000000010a7b4f36 -[UIControl _sendActionsForEvents:withEvent:] + 450 20 UIKitCore 0x000000010a7b3eec -[UIControl touchesEnded:withEvent:] + 583 21 UIKitCore 0x000000010ad97eee -[UIWindow _sendTouchesForEvent:] + 2547 22 UIKitCore 0x000000010ad995d2 -[UIWindow sendEvent:] + 4079 23 UIKitCore 0x000000010ad77d16 -[UIApplication sendEvent:] + 356 24 UIKitCore 0x000000010ae48293 __dispatchPreprocessedEventFromEventQueue + 3232 25 UIKitCore 0x000000010ae4abb9 __handleEventQueueInternal + 5911 26 CoreFoundation 0x0000000107062be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 27 CoreFoundation 0x0000000107062463 __CFRunLoopDoSources0 + 243 28 CoreFoundation 0x000000010705cb1f __CFRunLoopRun + 1231 29 CoreFoundation 0x000000010705c302 CFRunLoopRunSpecific + 626 30 GraphicsServices 0x000000010e96f2fe GSEventRunModal + 65 31 UIKitCore 0x000000010ad5dba2 UIApplicationMain + 140 32 timeschedle1 0x000000010460764b main + 75 33 libdyld.dylib 0x0000000108503541 start + 1 34 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
該当のソースコード
NextViewController.swift
swift
1 2import UIKit 3 4class NextViewController: UIViewController { 5 6 var clnamestring = "" 7 var clgroupstring = "" 8 var tanisuu = "" 9 var teacherstring = "" 10 var clnumber = "" 11 var zoomId = "" 12 var zoomPw = "" 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 // Do any additional setup after loading the view. 18 19 let changeButton = UIButton()//編集ボタン 20 changeButton.frame = CGRect(x: 300,y: 700,width: 100,height: 100) 21 changeButton.setTitle("編集", for: .normal) 22 changeButton.backgroundColor = UIColor.blue 23 changeButton.addTarget(self, action: #selector(NextViewController.changebutton), for: .touchUpInside) 24 view.addSubview(changeButton) 25 26 27 28 let navBar = UINavigationBar()//ナビゲーションバー 29 navBar.frame = CGRect(x: 0,y: 50,width: 415,height: 60) 30 let navItem : UINavigationItem = UINavigationItem(title: "月曜1限") 31 navItem.leftBarButtonItem = UIBarButtonItem(title: "戻る",style: UIBarButtonItem.Style.plain, target: self, action: #selector(NextViewController.backvcbutton)) 32 navBar.pushItem(navItem, animated: true) 33 self.view.addSubview(navBar) 34 35 36 /*let clnamelabel = UILabel()//授業 37 clnamelabel.frame = CGRect(x: 50,y: 112,width: 60,height: 15) 38 clnamelabel.text = "授業名" 39 self.view.addSubview(clnamelabel)*/ 40 let clname = UILabel()//授業名表示ラベル 41 clname.frame = CGRect(x:50,y: 130,width: 315,height: 55) 42 clname.text = clnamestring 43 clname.backgroundColor = UIColor.lightGray 44 self.view.addSubview(clname) 45 46 let clgroupname = UILabel()//授業分類ラベル 47 clgroupname.frame = CGRect(x:50,y: 200,width: 180,height: 30) 48 clgroupname.text = clgroupstring 49 clgroupname.backgroundColor = UIColor.lightGray 50 self.view.addSubview(clgroupname) 51 52 let tani = UILabel()//単位数表示ラベル 53 tani.frame = CGRect(x: 240,y:200,width: 80,height: 30) 54 tani.text = tanisuu 55 tani.backgroundColor = UIColor.lightGray 56 self.view.addSubview(tani) 57 58 /*let tanilabel = UILabel()//単位 59 tanilabel.frame = CGRect(x: 330,y: 200,width: 50,height: 30) 60 tanilabel.text = "単位" 61 self.view.addSubview(tanilabel)*/ 62 63 let teachername = UILabel()//教授名 64 teachername.frame = CGRect(x: 50,y: 250,width: 150,height: 30) 65 teachername.text = teacherstring 66 teachername.backgroundColor = UIColor.lightGray 67 self.view.addSubview(teachername) 68 69 let teacherlabel = UILabel() 70 teacherlabel.frame = CGRect(x: 50,y: 290,width: 50,height: 30) 71 teacherlabel.text = "教授名" 72 teacherlabel.backgroundColor = UIColor.lightGray 73 teacherlabel.addSubview(teacherlabel) 74 75 let clNumber = UILabel()//教室番号 76 clNumber.frame = CGRect(x: 210,y: 250,width: 150,height: 30) 77 clNumber.text = clnumber 78 clNumber.backgroundColor = UIColor.lightGray 79 self.view.addSubview(clNumber) 80 81 let zoomID = UILabel()//ZOOM ID 82 zoomID.frame = CGRect(x: 50,y: 300,width: 150,height: 50) 83 zoomID.text = zoomId 84 zoomID.backgroundColor = UIColor.lightGray 85 self.view.addSubview(zoomID) 86 87 let zoomPW = UILabel()//ZOOM PW 88 zoomPW.frame = CGRect(x: 210,y: 300,width: 150,height: 50) 89 zoomPW.text = zoomPw 90 zoomPW.backgroundColor = UIColor.lightGray 91 self.view.addSubview(zoomPW) 92 93 94 95 96 97 98 99 } 100 101 @objc func changebutton(sender: UIButton){ 102 let storyboard :UIStoryboard = self.storyboard! 103 let changeview = storyboard.instantiateViewController(withIdentifier: "change")as! ChangeViewController 104 self.present(changeview,animated: true,completion: nil) 105 106 107 } 108 @objc func backvcbutton(_ sender:UIButton){ 109 110 let storyboard :UIStoryboard = self.storyboard! 111 let changeview = storyboard.instantiateViewController(withIdentifier: "vc")as! ViewController 112 changeview.clName = self.clnamestring 113 self.present(changeview,animated: true,completion: nil) 114 115 116 117 } 118 119 120}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/23 04:17