2016-09-18 60 views
0

如何可以调用从方法URL或NSString的另一类我怎样才能调用URL从另一个类

1类

+ (NSURL *)createRequestURLWithChannel:(NSString *)channelName { 
    NSString *sName = [channelName stringByReplacingOccurrencesOfString:@"_" withString:@"-"]; 
    NSString *channelvideo = [NSString stringWithFormat:@"%@", sName]; 
    return [NSURL URLWithString:url]; 
} 

2类

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     if (PlayRowIndex == indexPath.row) { 

      NSURL *videoURL = [NSURL URLWithString:channelvideo]; 
      AVPlayer *player = [AVPlayer playerWithURL:videoURL]; 
      AVPlayerViewController *playerViewController = [AVPlayerViewController new]; 
      playerViewController.player = player; 
      [self presentViewController:playerViewController animated:YES completion:nil]; 


     } else if (StopRowIndex == indexPath.row) { 
     // Row stop 
     } else if (RowIndex == indexPath.row) { 
     // Row 3 
     } 

     [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    } 

我想呼叫URL channelvideo从类别1中的方法(createRequestURLWithChannel)并且在类别2中使用它的方法(tableView

+0

你需要将class 1的标题导入到class 2中,然后创建class 1的实例 –

+0

** NSNotificationCenter ** –

+0

这是一个实例方法s o你不需要创建任何对象。你可以简单地通过导入你的类来使用这个方法。 #import“Class1.h” 并使用这样的方法.. NSURL * url = [Class1 createRequestURLWithChannel @“ChannelName”]; –

回答

0

OOP(面向对象编程)的方式并不是这样,你可以像这样调用另一个类的函数。

例如,假设我们有一个班:“橙色”。

Orange的源代码告诉计算机如何创建一个Orange和Orange应该做什么。

现在我们还有一堂课:“香蕉”。

在香蕉中,我们想要使用橙色的方法(功能)。 要做到这一点,我们必须导入橙色:

// in banana.h or banana.m 
#import "Orange.h" 

现在我们必须创建橙色:

Orange *myOrange = [[Orange alloc] init]; 

最后调用它的方法:

NSSmoothie *mySmoothie = [myOrange makeSmoothieWith: self]; 
+0

也注意到你必须在Orange.h中定义Orange的方法 –

相关问题