开发者

missing copy/paste menu in UITextField/UIWebView

I'm having a problem and I can't find a workaround for it.

I have a view with a UIWebView implementation and a UITextField. On both, when I tap, I do not get the copy/paste menu to appear.

The UIWebView has text in it (text only) and I can select either a word, or a paragraph or make the magnification glass appear and manually select the text I want.

On the other hand, UITextField can take input and works as intended, except for the copy/paste functions.

Nothing is sub-classed. I need only the default implementation of iOS for the copy/paste functions.

This problem is not in a single view. I have another UIWebView with the same problem elsewhere开发者_开发问答 so I think this is a global problem.

I have done all the obvious (import UIKit and Foundation frameworks, assign properties, releasing etc) but again I'm stuck on this.

What might interact/interfere with such a simple functionality, disabling it? Moreover, always under the simplest implementation, what else is needed for this to work? (any framework I'm missing, any property etc).

Such a simple thing and I'm stuck with it. If anyone has any idea its much appreciated.

==== Edit ====

The problem is not caused by my code in any view or class.

I have added a new view (the application is tabbar based) with only a UITextField and a UITextView with the default "Lorem Ipsum" text. On the textView I can also select text but no menu to copy/paste/select/select All. This also happens in the textField (empty) where no paste menu appears (I copy some text from another app, ex. Safari or Notes).

It seems the problem is somewhere else affecting universally the app, in all views.

I have removed Frameworks references and put them back but nothing happened. I'm still trying to figure from where this comes.


I had the same problem – everything else was working correctly, but all UITextViews were missing "copy / paste / select" menu, globally, through all application.

After some experimenting, I've found that the cause was either:

"Visible at Launch" property not set on a Window in MainWindow.xib

OR

Missing call to [self.window makeKeyAndVisible] inside application:didFinishLaunchingWithOptions: method of AppDelegate.

It works fine after fixing any of those. Try it.


If you work with Xcode 11+ and iOS 13+ projects (and SceneDelegate),

make sure that you apply changes in that exact order:

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow()
        window!.rootViewController = rootNavigation
        window!.windowScene = windowScene
        window!.makeKeyAndVisible()
    }
}

because, if you will apply it something like that:

    window!.makeKeyAndVisible()
    window!.windowScene = windowScene

you will have described in question problem. It will appear and look fine, work, but copy-paste actions will not appear. It took me a while to find this out.


Make sure you implement:

//init your menu somewhere, appropiately

- (id) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
        if (!items) items = [[NSMutableArray alloc] init];

        UIMenuItem *menuItem;
        menuItem = [[UIMenuItem alloc] initWithTitle:@"Undo" action:@selector(undo:)];
        [items addObject:menuItem];
        [menuItem release];
        menuItem = [[UIMenuItem alloc] initWithTitle:@"Redo" action:@selector(redo:)];
        [items addObject:menuItem];
        [menuItem release];

        [[UIMenuController sharedMenuController] setMenuItems:items];
            [items release];

    }
    return self;
}

//allow other items to appear and yours too :) Perhaps you are missing this?
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if ([super canPerformAction:action withSender:sender]) {
        return YES;
    }
    else {
        //write your code here
        if (action == @selector(undo:) && [self.undoManager canUndo]) return YES;
        if (action == @selector(redo:) && [self.undoManager canRedo]) return YES;
    }
    return NO;
}

//Do your actions 
- (void)undo:(id)sender{
//do your stuff here
}
- (void)redo:(id)sender{
//do your stuff here
}


OK, I found the solution. This has nothing to do with the code or the properties of the objects. Its more or less an "implementation" of the XCode4.

When a new universal project is created, xcode puts an AppDelegate at the root of the project and 2 more AppDelegates in each folder, one for the iPad and another for the iPhone.

The root app delegate looks like

#import <UIKit/UIKit.h> 
@interface SampleAppDelegate : NSObject <UIApplicationDelegate> { }
@property (nonatomic, retain) IBOutlet UIWindow *window;

with

#import "SampleAppDelegate.h"
@implementation SampleAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
@end

The iPhone and iPad app Delegates have no 'window' reference.

If the root AppDelegate is deleted and the 'window' code is moved in each device-specific AppDelegate, the select/Select All/copy/paste (or any other option) menu appears again.

This happened to me on the first new xcode4 I created. I cant remember well if xcode3 had a similar implementation (but I think not).


Looks like, now (2020) there is bug into iOS. When you change alignment of the UITextField to center (NSTextAlignmentCenter) you can't trigger actions like paste for empty field.

Maybe it was also Your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜