Sorry for the long question, but I have been stuck on this for days and have exhausted all other help.
Currently, I have a tab bar application with four tabs. In the second tab (SecondViewController), I have a segmented controller at the top that should switch between “videos” and “images”. The videos page should have around 5 youtube videos loaded in UIWebView using the code here. The images view should contain around 5 thumbnails that, when clicked on, open into a larger picture. My problem is that I have tried out many different ways of accomplishing this, and none seem to work to any extent. Really the main thing I am looking for here is the recommended way of going about switching between two views using a segmented controller and if it is possible to load the views from different files (videosView.h/m and imagesView.h/m).
In SecondViewController.m, I have the app respond to the UISegmentedController using the following, though I have absolutely no idea if this is even close to correct.
- (IBAction)segmentedControlChanged
{
switch (segmentedControl.selectedSegmentIndex)
{case 0:
[self.view addSubview:videosView.view];
[imagesView.view removeFromSuperview];
NSLog(@"1");
break;
case 1:
[self.view addSubview:imagesView.view];
[videosView.view removeFromSuperview];
NSLog(@"2");
break;
default:
break;
}
}
In videosView.h, I only have the following:
#import <UIKit/UIKit.h>
@interface videosView : UIWebView
{
}
- (videosView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
@end
In videosView.m, I have the following, though I am getting a warning on the initWithFrame line.
- (videosView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// Create webview with requested frame size
self = [[UIWebView alloc] initWithFrame:frame];
// HTML to embed YouTube video
NSString *youTubeVideoHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
@end
imagesView is made, but has no added code it in currently, as I am just trying to get the videos sorted out first.
My recommendation:
UIView).addSubview:andremoveFromSuperView:, but rather set these “container” view ashiddenas appropriate.segmentedControlChangedmethod, do all the other necessary switching tasks, such as canceling open URL connections etc.viewDidLoadrather than in the initializer. Make sure you do not freeze the UI but use asynchronous loading.EDIT: Adding subclassing code.
in SecondViewController.h:
in SecondViewController.m
Whenever you want to execute view specific code, just call any method you define in VideosView.h and implement in .m.