Hi guys I'm new to objective c and i was searching for an c++ or java like way to initialize an objects properties from another UIViewController
class.
So I used the -init function which seems to work terrific, but the problem is that after some debugging I did I found that even though my property (an NSString
) is successfully initialize in -init
when the viewDidLoad
starts it deletes (initialize) everything I did in init so I can't use my property
! I want to play a video link but I found that the string that goes to playTheVideo
method is null. Also note that when I initialize the stream inside viewDidLoad
my video plays right away.
Here is my code:
ButtonsController.m
#import "ButtonsController.h"
#import "PlayVideoController.h"
@interface ButtonsController ()
@end
@implementation ButtonsController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
**UPDATE:[self createButton];**
}
-(void)createButton {
PlayVideoController *newObject = [[PlayVideoController alloc] initWithString:@"--somestring--" ];
[newObject playTheVideo];
}
PlayVideoController.m
#import "PlayVideoController.h"
@interface PlayVideoController ()
@end
@implementation PlayVideoController
@synthesize stream;
@synthesize player;
- (void)viewDidLoad
{
[super viewDidLoad];
[self playTheVideo];
}
- (id) initWithString: (NSString*) theStream {
self = [super init];
if (self) {
stream = theStream;
}
return self;
}
- (void) playTheVideo {
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[player loadRequest:request];
}
PlayVideoController.h
#import <UIKit/UIKit.h>
@interface PlayVideoController : UIViewController
@property NSString *stream;
@property (weak, nonatomic) IBOutlet UIWebView *player;
- (void) playTheVideo;
- (id) initWithString: (NSString*) theStream;
@end
I create an object in main class just to start it quickly
EDIT: deleted actions in main.m and left the defaults