在scrollview问题中更改页面

时间:2017-07-20 02:55:17

标签: ios swift xcode

我目前正在尝试添加一个按钮,以便我可以转到UIScrollView中的其他页面,但我一直收到错误。基本上我正在使用scrollview创建自定义幻灯片,占据屏幕底部的大约三分之一。

  

[App.ViewController aButton:]:发送到实例的无法识别的选择器   0x7fe464c131c0 2017-07-19 22:47:48.791 App [10680:546634] ***   由于未捕获的异常而终止应用程序   'NSInvalidArgumentException',原因:' - [App.ViewController aButton:]:   无法识别的选择器发送到实例0x7fe464c131c0'

到目前为止,我已经在这里阅读了几篇帖子并试过了

  

pagecontrol.currentpage = 0

  

mainScrollView.contentOffset = CGPoint(x:   mainScrollView.frame.size.width * 1,y:0)

import UIKit
import AVFoundation

extension UITextView: UITextViewDelegate{}

var audioPlayer = AVAudioPlayer()

var switchison = false;

var appArray: [Int] = []

var textArray: [String] = []


class ViewController: UIViewController,UIScrollViewDelegate {



    @IBOutlet weak var myTextView: UITextView!

    @IBOutlet weak var pageControl: UIPageControl!

    @IBOutlet weak var mainScrollView: UIScrollView!

    @IBAction func oneTap(_ sender: UISwitch) {
        if (sender.isOn == true)
        {
            switchison = true;
        }
        else
        {
            switchison = false;
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        mainScrollView.delegate = self
        let slides:[Slide] = createSlides()
        setupSlideScrollView(slides: slides)
        pageControl.numberOfPages = slides.count
        pageControl.currentPage = 0;
        view.bringSubview(toFront: pageControl)
    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func createSlides() -> [Slide]
    {
        let homeSlide = Bundle.main.loadNibNamed("Slide", owner: self, 
        options: nil)?.first as! Slide
        let aSlide = Bundle.main.loadNibNamed("SlideA", owner: self, 
        options: nil)?.first as! Slide
        let bSlide = Bundle.main.loadNibNamed("SlideB", owner: self, 
        options: nil)?.first as! Slide
        let cSlide = Bundle.main.loadNibNamed("SlideC", owner: self, 
        options: nil)?.first as! Slide
        let dSlide = Bundle.main.loadNibNamed("SlideD", owner: self, 
        options: nil)?.first as! Slide
        let eSlide = Bundle.main.loadNibNamed("SlideE", owner: self, 
        options: nil)?.first as! Slide
        let fSlide = Bundle.main.loadNibNamed("SlideF", owner: self, 
        options: nil)?.first as! Slide
        let gSlide = Bundle.main.loadNibNamed("SlideG", owner: self, 
        options: nil)?.first as! Slide
        let hSlide = Bundle.main.loadNibNamed("SlideH", owner: self, 
        options: nil)?.first as! Slide
        let iSlide = Bundle.main.loadNibNamed("SlideI", owner: self, 
        options: nil)?.first as! Slide
        let jSlide = Bundle.main.loadNibNamed("SlideJ", owner: self, 
        options: nil)?.first as! Slide
        let kSlide = Bundle.main.loadNibNamed("SlideK", owner: self, 
        options: nil)?.first as! Slide
        let lSlide = Bundle.main.loadNibNamed("SlideL", owner: self, 
        options: nil)?.first as! Slide
        let mSlide = Bundle.main.loadNibNamed("SlideM", owner: self, 
        options: nil)?.first as! Slide
        let nSlide = Bundle.main.loadNibNamed("SlideN", owner: self, 
        options: nil)?.first as! Slide
        let oSlide = Bundle.main.loadNibNamed("SlideO", owner: self, 
        options: nil)?.first as! Slide
        let pSlide = Bundle.main.loadNibNamed("SlideP", owner: self, 
        options: nil)?.first as! Slide
        let qSlide = Bundle.main.loadNibNamed("SlideQ", owner: self, 
        options: nil)?.first as! Slide
        let rSlide = Bundle.main.loadNibNamed("SlideR", owner: self, 
        options: nil)?.first as! Slide
        let sSlide = Bundle.main.loadNibNamed("SlideS", owner: self, 
        options: nil)?.first as! Slide
        let tSlide = Bundle.main.loadNibNamed("SlideT", owner: self, 
        options: nil)?.first as! Slide
        let uSlide = Bundle.main.loadNibNamed("SlideU", owner: self, 
        options: nil)?.first as! Slide
        let vSlide = Bundle.main.loadNibNamed("SlideV", owner: self, 
        options: nil)?.first as! Slide
        let wSlide = Bundle.main.loadNibNamed("SlideW", owner: self, 
        options: nil)?.first as! Slide
        let xSlide = Bundle.main.loadNibNamed("SlideX", owner: self, 
        options: nil)?.first as! Slide
        let ySlide = Bundle.main.loadNibNamed("SlideY", owner: self, 
        options: nil)?.first as! Slide
        let zSlide = Bundle.main.loadNibNamed("SlideZ", owner: self, 
        options: nil)?.first as! Slide
        return [homeSlide, aSlide, bSlide, cSlide, dSlide, eSlide, 
        fSlide, gSlide, hSlide, iSlide, jSlide, kSlide, lSlide, mSlide, 
        nSlide, oSlide, pSlide, qSlide, rSlide, sSlide, tSlide, uSlide, 
        vSlide, wSlide, xSlide, ySlide, zSlide,]
     }

     func setupSlideScrollView(slides:[Slide])
     {
        mainScrollView.frame = CGRect(x: 0, y: view.frame.height/1.5, 
        width: view.frame.width, height: view.frame.height)
        mainScrollView.contentSize = CGSize(width: view.frame.width * 
        CGFloat(slides.count), height: view.frame.height)
        mainScrollView.isPagingEnabled = true

        for i in 0 ..< slides.count
        {
            slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), 
            y: 0, width: view.frame.width, height: view.frame.height)
            mainScrollView.addSubview(slides[i])
        }
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pageIndex = 
        round(scrollView.contentOffset.x/view.frame.width)
        pageControl.currentPage = Int(pageIndex)
    }
    @IBAction func refreshButton(_ sender: Any) {
        myTextView.text = ""

        for words in textArray
        {
            myTextView.textStorage.append(NSAttributedString(string: 
            words))
        }
    }



    @IBAction func aAlphabetButton(_ sender: Any) {
        //need code here       
    }
}

3 个答案:

答案 0 :(得分:0)

请参阅? unrecognized selector sent to instance xxx这意味着您有一个不在某个控制器上实现的方法。

reason: '-[App.ViewController aButton:]: unrecognized selector sent to instance 0x7fe464c131c0'表示aButton:App.ViewController的方法未实现,或者您调用App.ViewController不包含此方法

答案 1 :(得分:0)

请检查所有与XIB连接的IBOutlets和IBActions是否正确连接。由于连接问题,此问题触发了。

答案 2 :(得分:0)

没有名为aButton:的方法。我认为你应该检查你是否已经定义了这样的方法。我可以看到一个名为aAlphabetButton的方法。您应该检查您的IB并检查是否与方法aButton相关联的任何按钮。可能会发生您之前定义的但已删除但忘记从按钮中删除IBAction。

有一个UIPageViewController用于您的目的,而不是手动创建幻灯片并添加到scrollview。