【问题标题】:UISegmented Controller to load one view with a few Youtube UIWebViews and another view with ImagesUISegmented Controller 加载一个带有几个 Youtube UIWebViews 的视图和另一个带有图像的视图
【发布时间】:2011-12-13 21:41:43
【问题描述】:

很抱歉这个问题太长了,但我已经坚持了好几天了,并且已经用尽了所有其他帮助。

目前,我有一个带有四个标签的标签栏应用程序。在第二个选项卡(SecondViewController)中,我在顶部有一个分段控制器,它应该在“视频”和“图像”之间切换。视频页面应该使用代码here 在 UIWebView 中加载大约 5 个 youtube 视频。图像视图应包含大约 5 个缩略图,单击时会打开更大的图片。我的问题是我已经尝试了许多不同的方法来实现这一点,但似乎都没有在任何程度上起作用。我在这里寻找的主要内容是推荐的使用分段控制器在两个视图之间切换的方法,以及是否可以从不同的文件(videosView.h/m 和 imagesView.h/m)加载视图。

在 SecondViewController.m 中,我让应用程序使用以下内容响应 UISegmentedController,但我完全不知道这是否接近正确。

- (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;
    }
}

在 videosView.h 中,我只有以下内容:

#import <UIKit/UIKit.h>

@interface videosView : UIWebView 
{
}

- (videosView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;

@end

在 videosView.m 中,我有以下内容,但我在 initWithFrame 行上收到警告。

- (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 已制作,但目前没有添加代码,因为我只是想先整理视频。

【问题讨论】:

    标签: iphone objective-c ios uiwebview uisegmentedcontrol


    【解决方案1】:

    我的建议:

    1. 使用一个视图控制器并让视图控制器包含两个视图。你已经在这样做了。
    2. 您仍然可以使用单独的文件(UIView 的子类)。
    3. 不要使用addSubview:removeFromSuperView:,而是根据需要将这些“容器”视图设置为hidden
    4. 此外,在segmentedControlChanged 方法中,执行所有其他必要的切换任务,例如取消打开的 URL 连接等。
    5. viewDidLoad 中而不是在初始化程序中初始化容器视图的Web 内容。确保您没有冻结 UI,而是使用异步加载。

    编辑:添加子类化代码。

    在 SecondViewController.h 中:

    #include VideosView.h    
    ...
    @property (nonatomic, retain) VideosView *videoView;
    

    在 SecondViewController.m 中

     -(void)viewDidLoad {  
       self.videosView = [[VideosView alloc] init]; 
       // add to superview etc.
    }
    

    当你想执行特定视图的代码时,只需调用你在 VideosView.h 中定义并在 .m 中实现的任何方法。

    [self.videosView playVideo];
    

    【讨论】:

    • 1.您是否建议我在 SecondViewController.h 中定义 UIView* videosView? 2. 我如何将它与您的第一次推荐联系起来? 3. 我该怎么做呢? 4.这是我第一次听说这个,我会尝试做那个。 5. 我不完全理解你的意思。正如您可能知道的那样,我远不是一个有经验的程序员,我很感激我能得到的所有帮助。
    • 1.是的。 2. 用括号回答。 3.videoView.hidden = !videoView.hidden; photoView.hidden = !photoView.hidden; 4.好的。 5. 言出必行。
    • 好吧,如果我在 SecondViewController 的实现中有 UIView *videosView 和 UIView *imagesView,我如何让这些视图假定来自 videosView.h/m 和 videosView.h/m 的属性?
    • interface* 抱歉我不是说实现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2018-10-09
    • 2018-11-13
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    相关资源
    最近更新 更多