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

Karthik Kumar
  • 1,375
  • 1
  • 12
  • 29
Alex
  • 111
  • 1
  • 1
  • 4
  • Please provide more description in your question. Otherwise it will be removed as low quality – T.S. Jun 14 '14 at 20:23
  • Hi T.S. I added more description on it. Please let me know if it is enough. – Alex Jun 14 '14 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. – jrturton Jun 15 '14 at 08:12
  • How can I connect the outlet ? – Alex Jun 15 '14 at 12:11

3 Answers3

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)
    }

}
Community
  • 1
  • 1
Ryan
  • 3,853
  • 4
  • 28
  • 32
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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • 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. – Alex Jun 15 '14 at 12:08
  • 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. – jrturton Jun 15 '14 at 17:23
  • 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 – Alex Jun 16 '14 at 02:37
  • That sounds like you don't have the class of the view controller set properly in the identity inspector. – jrturton Jun 16 '14 at 06:24
  • In the identify inspector the class it is identified as "UIViewController" do I need to change or do any thing more? – Alex Jun 16 '14 at 11:59
  • Yes, you have to set it as `ViewController` which is the custom class you are working with, otherwise, it won't know the object has a `scroller` outlet – jrturton Jun 16 '14 at 12:44
  • 1
    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. – Alex Jun 16 '14 at 13:27
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);

    }
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51