8

I would like to get the text copied to clipboard when application launching.

I can use following text to get the available text from clipboard. But I need to use this value in a different viewcontroller. How can I pass this value to my viewcontroller?

- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSLog([UIPasteboard generalPasteboard].string);

}
4

3 回答 3

10

更好的处理方法是为 UIApplicationDidBecomeActiveNotification 事件添加一个观察者(在视图控制器中)。这样可以避免应用程序委托和视图控制器之间不必要的耦合。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(getClipboardString:)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];

编辑:当视图控制器被移除时,不要忘记移除观察者:

[[NSNotificationCenter defaultCenter] removeObserver:self];
于 2013-07-28T19:45:39.983 回答
9

在您的 VC 上声明并实现一个方法,您可以在激活时从应用程序委托调用该方法:

@inferface ViewController: UIViewController {
   /* etc. */
}

- (void)handlePasteboardString:(NSString *)s;

@end

在您的应用委托中:

- (void)applicationDidBecomeActive:(UIApplication *)a
{
    [self.mainViewController handlePasteboardString:[UIPasteboard generalPasteboard].string];
}
于 2012-08-04T13:14:57.623 回答
0

不确定何时需要剪贴板项目,但如果是在显示或即将显示特定视图控制器时,您可以在viewDidLoad该视图控制器中执行此操作

于 2016-05-09T19:32:38.767 回答