Динамическое создание класса в Objective C

В моем случае, вот шаги.

Шаг-1

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Here notify the fragment that it should participate in options menu handling.
    setHasOptionsMenu(true);
}

Шаг-2

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // First clear current all the menu items
    menu.clear();

    // Add the new menu items
    inflater.inflate(R.menu.post_stuff, menu);

    super.onCreateOptionsMenu(menu, inflater);
}

Шаг-3

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.post_stuff:
            Log.d(TAG, "Will post the photo to server");
            return true;
        case R.id.cancel_post:
            Log.d(TAG, "Will cancel post the photo");
            return true;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
5
задан 23 June 2009 в 18:37
поделиться

1 ответ

Вам нужен NSClassFromString .

NSString *theClassForMe = @"NSMutableArray";
id newObject = [[NSClassFromString(theClassForMe) alloc] init];
13
ответ дан 18 December 2019 в 14:50
поделиться