UIViewComponent from scratch


#1

Howdy

Just trying to load an XCode 6 xib file into my juce app.

I'm starting from scratch (Introjucer -> New Gui Application) trying to get to the heart of what needs to be done.

 

Instead of using a subclass of DocumentWindow like the default application does, I subclassed UIViewComponent, call it iosWindow.
Then I include a custom UIView subclass (trying to tie that to a xib), and set up the pointer to it in the header.

#include "../JuceLibraryCode/JuceHeader.h"
#import "ViewTest1.h" //the custom UIView subclass

class iosWindow    : public UIViewComponent
{
public:
    iosWindow();
    ~iosWindow();

    void resized();
    ViewTest1 *viewTest;

private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (iosWindow)
};

Then as far as I've gotten, is the iosWindow constructor - pretty sure I need to create the uiview here, but not sure how to do it.

iosWindow::iosWindow()
{
    Logger::writeToLog("iosWindow");
    setSize (500, 400);
    
    //I tried this but it says incompatible type ViewTest1 * from ViewTest1 * ???
    /* viewTest = new ViewTest1(); */ 
    
    setView(viewTest);
    
}

...and of course I bring the iosWindow to life in the main file.

 

I am thinking there's some sort of initialisation I'm missing for my UIView itself? I've set the xib file's owner to my custom class, but I'm not sure where to go after that.

What do yall think?


#2

I got it (of course with new issues ;] ).

One thing I didn't clarify in the last post is it helps if your main file and the new uiviewcomponent subclass are .mm files - so you can write C++ for Juce and ObJ-C for ios side by side.

My new constructor for the ios juce component became:

iosWindow::iosWindow()
{    
    int width = Desktop::getInstance().getDisplays().getTotalBounds(false).getWidth();
    int height = Desktop::getInstance().getDisplays().getTotalBounds(false).getHeight();
    centreWithSize(width, height);
    
    viewTest = [[ViewTest1 alloc]initWithFrame:CGRectMake(0, 0, getWidth(), getHeight())]; //create instance custom uiview
    setView(viewTest);    
}

My problem was in the way the xib was connected - and the init function within my custom uiview subclass.

The xib has to have a view and a property for that view in the subclass header file.
The "File's owner" class has to be set to the custom class (NOT the view itself - this still needs to be set to UIView).
Then the custom uiview class needs this method:

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        //Load interface
        [[NSBundle mainBundle] loadNibNamed:@"ViewTest1" owner:self options:nil];

        //set size
        self.bounds = self.view.bounds;
        
        //add as subview
        [self addSubview:self.view];
        
    }
    [self viewDidLoad];
    return self;
}

It works - and I'm dealing now with trying to make it truly fullscreen.

If I set the mainWindow->setfullscreen(true), then it seems to work pretty well, except that black bar at the top for status bar. This happens even if 'status bar hidden' is ticked in the introjucer. Also if the 'status bar' is set to 'none in the xib editor.
Any ideas on that?

If setFullScreen is NOT called on the main app, then my window appears slightly offset from the top, and as I rotate the device, the window slowly sneaks up higher each time.

So does anyone know what is up with the top of the window not being correct?

Thanks - I hope this helps someone.


#3

I'm struggling to work this out. 

I created a subclass of UIView, TestView. So I have TestView.h and TestView.m files. I also creat a xib file for the interface and called it TestView1.xib

My TestView.m file looks like this: 

#import "TestView.h"
@implementation TestView

- (id)initWithFrame:(CGRect) frame {

    self = [super initWithFrame:frame];

    if (self) {
        //Load interface
        [[NSBundle mainBundle] loadNibNamed:@"TestView1" owner:self options:nil];
        
        //set size
        self.bounds = self.view.bounds; // Error: Property 'view' not found on object of type 'TestView *'

        //add as subview
        [self addSubview:self.view]; // Error: Property 'view' not found on object of type 'TestView *'

    }

    [self viewDidLoad];

    return self;
}

@end

However it complains that "Property 'view' not found on object of type 'TestView *'"

Maybe I put that initWithFrame method in the wrong place?

The part of the post above that I dont quite get is this:

The xib has to have a view and a property for that view in the subclass header file.  <-- not sure how to do this
The "File's owner" class has to be set to the custom class (NOT the view itself - this still needs to be set to UIView).

Reading around the subject it looks like 'view' is a property of UIViewController.

Does  JUCE create an instance of UIViewController in order to switch components etc on iOS?

Any help with this much appreciated!