9

I'm constructing my first App for IOS and I'm struggling to find a way to do a simple ScrollView using the Swift code on the XCode6, please can someone help me to find the solution?

My problem is that I don't know how to make the scrollview work in my code. I already putted the code as you can see below in the ViewController.swift and I was expecting to be able to select the Outlet "scroller" in the Main.storyboard for a ViewController, instead of this I'm receiving the error *"fatal error: Can't unwrap Optional.None (lldb)"* EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)

I have some ViewController screens and in one of that I putted one ScrollView and I want to make it works using the Swift.

I'm stuck on this:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var scroller:UIScrollView

    override func viewDidLoad() {
        super.viewDidLoad()
        scroller.scrollEnabled = true;
        scroller.contentSize = CGSizeMake(320, 624);
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

I think if someone can provide a simple example how to do a scrollview using swift it will solve my problem. Any help is appreciate.

Trying to do it in a old style I tried to do it using a .m and .h file:

ViewController.m

#import "Amigo-Bridging-Header.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 624)];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Amigo-Bridging-Header.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UIScrollView *scroller;
}
@end

Cheers

4
  • Please provide more description in your question. Otherwise it will be removed as low quality Commented Jun 14, 2014 at 20:23
  • Hi T.S. I added more description on it. Please let me know if it is enough. Commented Jun 14, 2014 at 21:43
  • Are you sure the outlet is connected? The error suggests that you've got a None value in an implicitly unwrapped optional, rather than any specific scroll view problem. Commented Jun 15, 2014 at 8:12
  • How can I connect the outlet ? Commented Jun 15, 2014 at 12:11

3 Answers 3

10

Let's give this a shot. The one thing to note is I have yet to find a way to downcast self.view as a UIScrollView, so you can't make calls like self.view.contentOffset.

import UIKit

class ScrollingViewController : UIViewController {
    // Create a scrollView property that we'll set as our view in -loadView
    let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)

    override func loadView() {
        // calling self.view later on will return a UIView!, but we can simply call 
        // self.scrollView to adjust properties of the scroll view:
        self.view = self.scrollView

        // setup the scroll view
        self.scrollView.contentSize = CGSize(width:1234, height: 5678)
        // etc...
    }

    func example() {
        let sampleSubView = UIView()
        self.view.addSubview(sampleSubView) // adds to the scroll view

        // cannot do this:
        // self.view.contentOffset = CGPoint(x: 10, y: 20)
        // so instead we do this:
        self.scrollView.contentOffset = CGPoint(x: 10, y: 20)
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the shot, but I think it not for what I'm trying to do.
0

Your outlet is not connected. From the Swift with objective-C book:

When you declare an outlet in Swift, the compiler automatically converts the type to a weak implicitly unwrapped optional and assigns it an initial value of nil. In effect, the compiler replaces @IBOutlet var name: Type with @IBOutlet weak var name: Type! = nil

If this value was not connected, it would remain as nil and you'd get a runtime error when accessing the value.

7 Comments

Hi Jrturton, this explain a lot why I'm with problems. I think I already have this book. Thank you for share. BUT how we can correct the code to have the Outlet? Do I need to do a bridge with .m(Objective C file)? I have the same code in a ViewController.m and a reference in the bridge header file.
There's nothing wrong with the code. You need to make sure you've connected the outlet in interface builder. Select the view controller object and check that the outlet is connected.
Wow, ok, the problem is exactly this, if I go to the interface builder and select the view controller and try to connect the outlet available, the "scroller" don't appear
That sounds like you don't have the class of the view controller set properly in the identity inspector.
Thanks, now I have it in available at the interface builder and I connected it, BUT I still receiving the error "fatal error: Can't unwrap Optional.None (lldb)" EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)" when I'm running the program.
|
0
 @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // setup the scroll view
        self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 200, 0);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.