Todos los productos
Search
Centro de documentos

Mobile Platform as a Service:Vista personalizada

Última actualización: Sep 17, 2026

Los miniprogramas admiten vistas personalizadas a partir de mPaaS 10.1.68.36.

Procedimiento

  1. Hereda la interfaz NBComponent.

     @interface CustomTestView : NBComponent
  2. Sobrescriba el siguiente método para devolver la vista creada en init.

     - (id)initWithConfig:(NSDictionary *)config messageDelegate:(id<NBComponentMessageDelegate>)messageDelegate {
         self = [super initWithConfig:config messageDelegate:messageDelegate];
         if (self) {
             self.contentSpaceView = [[UIView alloc] init];
             self.contentSpaceView.backgroundColor = [UIColor orangeColor];
             self.contentSpaceView.frame = CGRectMake(0, 0, 100, 100);
             UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(postMessage)];
             [self.contentSpaceView addGestureRecognizer:tap];
         }
         return self;
     }
     //Return the View created in `init`.
     - (UIView *)contentView {
         return self.contentSpaceView;
     }
  3. Reciba mensajes del miniprograma.

     - (void)componentReceiveMessage:(NSString *)message data:(NSDictionary *)data callback:(NBComponentCallback)callback {
         if ([message isEqualToString:@"setColor"]) {
             callback(@{@"success":@"1"});
         }else if ([message isEqualToString:@"startAnimation"]) {
             [self.nbComponentMessageDelegate sendCustomEventMessage:@"nbcomponent.mpaasComponent.customEvent" component:self data:@{@"sth":@"start"} callback:^(NSDictionary * _Nonnull data) {
    
             }];
         }
     }
  4. Envíe un mensaje al miniprograma.

     [self.nbComponentMessageDelegate sendCustomEventMessage:@"nbcomponent.mpaasComponent.customEvent" component:self data:@{
                 @"element":@"elementId",
                 @"eventName":@"onXxx",
                 @"data":{}
             } callback:^(NSDictionary * _Nonnull data) {
    
             }];

    A continuación se describen los parámetros:

    Parámetro

    Descripción

    element

    Identificador del elemento en la etiqueta.

    eventName

    Nombre del evento. Debe comenzar con on.

    data

    Parámetro del evento personalizado.

  5. Registre la vista personalizada.

     - (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
         [[PSDService sharedInstance] registerComponentWithName:@"componentName" clsName:@"className"];
    
     }
  6. Llame a la vista personalizada desde el miniprograma.

     <mpaas-component
       id="mpaas-map"
       type="custom_map"
       style="{{ width: 200, height: 200 }}"
     />

    La etiqueta mpaas-component es un valor fijo. No la modifique. Otros parámetros:

    Parámetro

    Descripción

    id

    Identificador de instancia de la vista personalizada. Debe ser único dentro de un miniprograma.

    type

    Debe coincidir con el componentName registrado en el cliente. Se recomienda agregar un prefijo.

    style

    Ancho y alto de la vista.

  7. Agregue parámetros personalizados al componente del miniprograma:

     <mpaas-component
       id="mpaas-map"
       type="custom_map"
       style="{{ width: 200, height: 200 }}"
       color="#FFFF00FF"
       ···
     />
    Importante
    • color es un parámetro de renderizado personalizado. Los nombres de estos parámetros se definen libremente.

    • id, type y style son campos reservados. No los utilice como nombres de parámetros de renderizado personalizados.

    • Los nombres de los parámetros personalizados no deben comenzar con on, y su tipo no debe ser func.

  8. Reciba los parámetros personalizados en el cliente.

     - (void)componentDataWillChangeWithData:(NSDictionary *)data {
    
     }
    
     - (void)componentDataDidChangeWithData:(NSDictionary *)data {
    
     }

Otros métodos internos del componente

// self.nbComponentMessageDelegate method
@protocol NBComponentMessageDelegate <NSObject>
@required
/**
 * The component actively sends a message to the page (Native->Page)
 *
 * @param message Message name.
 * @param component The component to send the message.
 * @param data Content of the message.
 * @param callback The callback after the page has processed the message.
 *
 * @return void
 */
- (void)sendMessage:(NSString *)message
          component:(id<NBComponentProtocol>)component
               data:(NSDictionary *)data
           callback:(NBComponentCallback)callback;
@optional
/**
 * Components can directly execute JS in the execution environment.
 *
 * @param javaScriptString JS to be executed.
 * @param completionHandler Executes the callback function.
 *
 * @return void
 */
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
/**
 * The component actively sends a message to the page (Native->Page).
 *
 * @param message Message name (message is not processed internally).
 * @param component The component to send the message.
 * @param data Content of message.
 * @param callback The callback after the page has processed the message.
 *
 * @return void
 */
- (void)sendCustomEventMessage:(NSString *)message
                     component:(id<NBComponentProtocol>)component
                          data:(NSDictionary *)data
                      callback:(NBComponentCallback)callback;
@end
@protocol NBComponentLifeCycleProtocol <NSObject>
- (void)componentWillAppear;
- (void)componentDidAppear;
/**
 * The component will be destroyed.
 *
 * @return void
 */
- (void)componentWillDestory;
/**
 * After the component is destroyed.
 *
 * @return void
 */
- (void)componentDidDestory;
- (void)componentWillResume;
- (void)componentDidResume;
- (void)componentWillPause;
- (void)componentDidPause;
//fullscreen
/**
 The component is about to enter the fullscreen callback.
 */
- (void)componentWillEnterFullScreen;
/**
 Callback when component enters fullscreen.
 */
- (void)componentWillExitFullScreen;
/**
 The component is about to exit the fullscreen callback.
 */
- (void)componentDidEnterFullScreen;
/**
 Component exits the callback of fullscreen.
 */
- (void)componentDidExitFullScreen;

//visiblity
/**
 The component is about to exit the fullscreen callback.
 */
- (void)componentDidHidden;
/**
 Component exits the callback of fullscreen.
 */
- (void)componentDidVisiblity;
@end
@protocol NBComponentDataProtocol <NSObject>
/**
 * Component data will be updated.
 *
 * @param data Data content.
 *
 * @return void
 */
- (void)componentDataWillChangeWithData:(NSDictionary *)data;
/**
 * The data of the component has been updated, it is generally necessary to update the interface or perform other operations of the component.
 *
 * @param data Data content.
 *
 * @return void
 */
- (void)componentDataDidChangeWithData:(NSDictionary *)data;
@end
@protocol NBComponentFullScreenProtocol <NSObject>
/**
 Whether it is in fullscreen mode.

 @return Whether it is in fullscreen mode.
 */
- (BOOL)isFullScreen;
/**
 @return If enter fullscreen mode is needed.
 */
- (BOOL)shouldEnterFullScreen;
/**
 Set whether the ContentView needs to be fullscreen, the business can switch to fullscreen mode by changing it.
 @param fullScreen Whether fullscreen is required.
 @param shouldRotate Whether you need to rotate the screen.
 */
- (void)setContentViewFullScreen:(BOOL)fullScreen shouldRotate:(BOOL)shouldRotate;
@end
@protocol NBComponentVisibilityProtocol <NSObject>
/**
 State of visibilityState.
 @return State of visibilityState.
 */
- (NBComponentVisibilityState)visibilityState;
/**
 Set the state of VisibilityState
 @param state VisibilityState
 @return set successfully or not
 */
- (BOOL)setVisibilityState:(NBComponentVisibilityState)state;
/**
 Business rewrites this method to return the result of monitoring visibility changes or not, the default is NO.
 @return if monitor the changing of visibility is needed.
 */
- (BOOL)shouldObServerVisibilityStateChange;
@end