> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Poll

> The poll form: a question and up to twelve options, hosted by the message composer or by your own screen.

`CometChatCreatePoll` is the form for a new poll: a question and its options. The
[message composer](https://www.cometchat.com/docs/ui-kit/ios/v6/message-composer.md) opens it from its **Poll** attachment
option and posts the poll for you. You can also host it yourself.

The form is not a connected component. It makes no network call: it hands the question and
options back when Send is tapped, and its host creates the poll and tells it how that went.

![CometChatCreatePoll showing a question and two answers](https://mintcdn.com/cometchat-22654f5b/pWDsskjSUHgot30F/images/ui-kit/ios/v6/docs-overview-create-poll.png?fit=max\&auto=format\&n=pWDsskjSUHgot30F\&q=85\&s=0030ac4a45af7b5dd49fba0614ed53dd)

***

## Where It Fits

> [tab] SwiftUI
>
> ```
> CometChatMessageComposer
>         │ attachment menu ▸ Poll       (hidePollOption: false)
>         ▼
> ┌────────────────────────────────┐
> │ CometChatCreatePoll   ← here   │   a sheet
> └────────────────────────────────┘
>         │ Send
>         ▼
>    the composer posts the poll with the Polls extension
> ```
>
> `CometChatCreatePoll` is a `View`.

> [tab] UIKit
>
> ```
> CometChatMessageComposer
>    │ attachment sheet ▸ Poll       (hidePollsOption = false)
>    ▼
> CometChatCreatePoll   ← you are here
>    │ onSubmitClick(question, options)
>    ▼
> MessageComposerStore.createPoll(…)   → the poll message arrives in the list
> ```
>
> `CometChatCreatePoll` is a `UIViewController`. Present it in a `UINavigationController`: it
> shows **Cancel** and **Send** in the bar, a QUESTION section and an OPTIONS section.

***

## Quick Start

### With the Composer

The composer's Poll option is hidden by default. Show it and the composer does the rest.

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatMessageComposer(group: group, hidePollOption: false)
> ```
>
> The composer opens the form in a sheet and posts the poll to the chat — quoted, if you are
> replying in this conversation. While it posts, the form shows "Creating Poll…" and the
> sheet cannot be dismissed. On success the sheet closes, and the poll arrives in the message
> list like any other message. A failure shows its message under the form and does not reach
> the composer's `onError`.
>
> The option is dropped inside a thread (see the composer's
> [menu caveat](https://www.cometchat.com/docs/ui-kit/ios/v6/message-composer.md#custom-options)).

> [tab] UIKit
>
> ```swift theme={null}
> composer.hidePollsOption = false
> ```
>
> A failure shows in the form and is also sent to the composer's `onError`.
>
> Use the form directly only to create polls from your own screen, or to replace the
> composer's default with `set(onPollClick:)`.

Polls need the **Polls** extension enabled on your CometChat dashboard.

### Host It Yourself

Handle the submit callback, post the poll, and tell the form how it went. The composer's
store has the call it uses.

> [tab] SwiftUI
>
> Handle `onSubmitClick`, post the poll, and pass `isSubmitting` and `errorMessage` back in.
>
> ```swift theme={null}
> struct PollSheet: View {
>     let store: MessageComposerStore
>     @Environment(\.dismiss) private var dismiss
>     @State private var submitting = false
>     @State private var error: String?
>
>     var body: some View {
>         CometChatCreatePoll(
>             isSubmitting: submitting,
>             errorMessage: error,
>             onSubmitClick: { question, options in
>                 submitting = true
>                 error = nil
>                 store.createPoll(
>                     question: question, options: options,
>                     onSuccess: { submitting = false; dismiss() },
>                     onError: { submitting = false; error = $0.message })
>             },
>             onBackPress: { dismiss() }
>         )
>     }
> }
> ```
>
> `createPoll` posts to the store's current user or group. With neither set, it fails with
> `ERR_NO_RECEIVER`.

> [tab] UIKit
>
> Present the form in a navigation controller, and create the poll when it submits. Here the
> composer's store makes the call:
>
> ```swift theme={null}
> final class TeamViewController: UIViewController {
>     private let store = MessageComposerStore()
>
>     func createPoll(in group: CometChatSDK.Group) {
>         store.setGroup(group)
>         let form = CometChatCreatePoll()
>         form.set(onSubmitClick: { [weak self, weak form] question, options in
>             guard let self, let form else { return }
>             form.set(progressVisible: true).set(errorStateVisible: false)
>             self.store.createPoll(question: question, options: options, onSuccess: {
>                 self.dismiss(animated: true)
>             }, onError: { error in
>                 form.set(progressVisible: false)
>                     .set(errorStateVisible: true)
>                     .set(errorMessage: error.message)
>             })
>         })
>         .set(onBack: { [weak self] in self?.dismiss(animated: true) })
>         present(UINavigationController(rootViewController: form), animated: true)
>     }
> }
> ```
>
> > [!NOTE]
> >
> > Until `set(onSubmitClick:)` has a callback, **Send does nothing**; until `set(onBack:)`
> > has one, **Cancel does nothing**. Passing `nil` to either is ignored and keeps the
> > previous callback.

***

## The Form

> [tab] SwiftUI
>
> The form has a navigation bar with **Cancel**, the title and **Send**, then a **Question**
> section and a **Set the answers** section. The form takes no focus by itself.
>
> ![The SwiftUI create-poll form filled in: Cancel, Poll and Send in the bar, a lunch question, two answers and an empty Add row that appeared once both were filled](https://mintcdn.com/cometchat-22654f5b/pWDsskjSUHgot30F/images/ui-kit/ios/v6/docs-pollSwiftUI.png?fit=max\&auto=format\&n=pWDsskjSUHgot30F\&q=85\&s=cba0d9ad33fc6e434fb6476be9d82572)
>
> * **The question** is multi-line. **Each option** is a single line.
> * **Two option rows** to start. When both are filled, a third appears. From then on,
>   typing in the last row adds another, up to **12**. At 12 there is no empty row.
> * **Removing:** there is no delete button. An empty row is removed when it loses focus,
>   unless it is the last row or only two rows are left. A row of spaces counts as blank and
>   is removed.
> * **Reordering:** long-press a filled row and drag it onto another filled row. The drag
>   handle shows on filled rows only.
> * The form scrolls to the bottom whenever rows are added or removed.
> * A row's focus belongs to its position, not its content. When an empty row loses focus to
>   the row below it and is removed, the focus moves to the next row down. For example,
>   tapping **D** in `A, (empty), C, D, (empty)` leaves the caret in the last, empty row.
>
> **Send** is enabled when the question is filled **and the first two rows are filled** —
> those two rows, not any two. It is re-checked on every change. The form does not reset
> after a send; the host decides.
>
> `onSubmitClick` receives the question, trimmed, and one entry per row **at the row's
> index**: the row's text, trimmed, or `nil` for a blank row. So `A`, blank, `C` arrives as
> `["A", nil, "C"]` — and the composer posts it that way, as a JSON `null` between the two
> options.

> [tab] UIKit
>
> The question takes focus when the form appears. Two empty options start the list. The
> question and the options are multi-line.
>
> ![The UIKit create-poll form filled in: Cancel, Poll and Send in the navigation bar, a Question section with a lunch question, and an Options section with two answers and an empty Add row](https://mintcdn.com/cometchat-22654f5b/pWDsskjSUHgot30F/images/ui-kit/ios/v6/docs-pollUIKit.png?fit=max\&auto=format\&n=pWDsskjSUHgot30F\&q=85\&s=e0f8efa609f40ece3c808687e5d34e50)
>
> | Action | Result |
> | - | - |
> | fill the first two options | a third, empty row is added |
> | type into the last row | another empty row is added, up to **12** |
> | leave an **empty** row | it is removed — unless it is the last, or only two rows remain |
> | long-press a filled row and drag | reorders it; a move from or onto an empty row is refused |
>
> The drag handle shows only on filled rows. There is no delete button. "Empty" means no
> characters at all: a row of spaces is kept.
>
> **Send** is enabled when the question and **the first two options** are non-blank. It is
> not re-checked when a row is removed or moved. It calls `onSubmitClick` with:
>
> * the question, trimmed;
> * each **non-empty** option, trimmed, **at its row index** — an empty row between filled
>   ones leaves a `nil` there, and a row of spaces is sent as `""`.
>
> ```swift theme={null}
> let form = CometChatCreatePoll()
> form.set(onSubmitClick: { question, options in
>     let answers = options.compactMap { $0 }.filter { !$0.isEmpty }
>     track("\(question): \(answers.count) options")
> })
> ```
>
> The composer's store sends the options as given, `nil` as JSON `null`.

***

## Actions and Events

### Callback Methods

> [tab] SwiftUI
>
> | Parameter | Signature | When |
> | - | - | - |
> | `onSubmitClick` | `(question, options)` | Send is tapped — see above |
> | `onBackPress` | `()` | Cancel is tapped. Cancel can be tapped only when this is set |

> [tab] UIKit
>
> | Setter | Signature | When |
> | - | - | - |
> | `set(onSubmitClick:)` | `(String, [String?]) -> Void` | Send is tapped |
> | `set(onBack:)` | `() -> Void` | Cancel is tapped |
>
> `onSubmitClick` and `onBack` can also be read back.

***

## Functionality

> [tab] SwiftUI
>
> | Parameter | Default | Effect |
> | - | - | - |
> | `title` | `nil` — "Poll" | the navigation bar's title |
> | `hideToolbar` | `false` | hides the title and Cancel. Send stays, since it submits the form |
> | `isSubmitting` | `false` | disables Send and shows "Creating Poll…" over the form, which also stops the sheet from being dismissed |
> | `errorMessage` | `nil` | shown in a banner under the form |
> | `onSubmitClick` | `nil` | `(question, options)` — see above |
> | `onBackPress` | `nil` | Cancel |
> | `style` | `CometChatCreatePollStyle()` | see Style |

> [tab] UIKit
>
> The form shows what the host tells it:
>
> | Setter | Shows |
> | - | - |
> | `set(progressVisible: true)` | the "Creating Poll…" alert over the form; Send disabled and without its title |
> | `set(progressVisible: false)` | removes the alert and **re-enables Send**, whether or not the form is valid |
> | `set(errorStateVisible:)` | the error banner under the table |
> | `set(errorMessage:)` | the banner's text |
> | `set(sendButtonTextVisible:)` | Send's title |
> | `set(title:)` | the title — "Poll" by default |
> | `set(hideToolbar:)` | `true` hides the title and **Cancel** and keeps **Send** |
>
> While the alert is up the sheet cannot be dismissed. The form never resets, retries or
> closes by itself.
>
> ```swift theme={null}
> let form = CometChatCreatePoll()
> form.set(title: "Team vote")
> form.set(hideToolbar: true)
> ```
>
> | Member | Purpose |
> | - | - |
> | `resetForm()` | an empty question, two empty options, the banner hidden; the alert is left as it is |
> | `question` | the question as typed, untrimmed |
> | `options` | every row as typed, empty ones included |
> | `tableView`, `sendButton`, `cancelButton`, `titleLabel`, `errorView`, `messageLabel` | the form's views |

***

## Advanced Methods

> [tab] SwiftUI
>
> `CometChatCreatePoll` is a `View` struct. Customise it with its parameters and style.

> [tab] UIKit
>
> ### Subclassing
>
> The class is `open`. These are its override points:
>
> | Method | Called |
> | - | - |
> | `buildUI()` | once, to lay out the table and the error banner |
> | `setupStyle()` | on load, on appearing, and when `style` or `theme` changes |
> | `registerCells()` | once, to register the question and option cells |
> | `setupNavigation()` | once, to put Cancel, the title and Send in the navigation bar |
> | `numberOfSections(in:)`, `tableView(_:numberOfRowsInSection:)`, `tableView(_:cellForRowAt:)`, `tableView(_:viewForHeaderInSection:)` | the table's data source |
>
> ```swift theme={null}
> final class BrandedPollForm: CometChatCreatePoll {
>     override func setupStyle() {
>         super.setupStyle()
>         tableView.backgroundColor = .systemGroupedBackground
>     }
> }
> ```
>
> ### Objective-C
>
> The form is `@objc`. `set(title:)` is `setWithTitle:` — `setTitle:` is
> `UIViewController`'s — and a gap in the options arrives as `NSNull`:
>
> ```objc theme={null}
> CometChatCreatePoll *form = [[CometChatCreatePoll alloc] init];
> [form setOnSubmitClick:^(NSString *question, NSArray *options) {
>     NSLog(@"%@: %lu options", question, (unsigned long)options.count);
> }];
> [form setOnBack:^{ NSLog(@"cancelled"); }];
> [form setWithTitle:@"Team vote"];
>
> CometChatCreatePollStyle *style = [[CometChatCreatePollStyle alloc] init];
> style.sendButtonTextColor = UIColor.systemIndigoColor;
> form.style = style;
> ```
>
> In Objective-C the style is `CometChatCreatePollStyle`.

***

## Accessibility

In the SwiftUI toolkit:

* The question, each option and each drag handle have VoiceOver labels. Send says whether
  it is disabled.
* The "Creating Poll…" alert has its own VoiceOver label.

***

## Style

> [tab] SwiftUI
>
> `CometChatCreatePollStyle` is sparse: set only what you change, and everything else follows
> the theme.
>
> ```swift theme={null}
> var style = CometChatCreatePollStyle()
> style.questionBackgroundColor = .yellow.opacity(0.1)
> style.submitButtonTextColor = .indigo
>
> CometChatCreatePoll(style: style, onSubmitClick: { question, options in log(question) })
> ```
>
> | Property | Default |
> | - | - |
> | `backgroundColor` | `background1` |
> | `cornerRadius`, `strokeWidth`, `strokeColor` | the whole form's corner and border — none, 0, `strokeLight` |
> | `titleTextColor`, `titleTextStyle` | `textPrimary`, `.headline` |
> | `questionTitleTextColor`, `…TextStyle`; `optionTitleTextColor`, `…TextStyle` | the section headers — `textPrimary`, `heading4Regular` |
> | `questionTextColor`, `questionTextStyle`, `questionHintColor` | `textPrimary`, `bodyMedium`, `textTertiary` |
> | `questionBackgroundColor`, `…CornerRadius`, `…StrokeWidth`, `…StrokeColor` | `background3`, 8, 0, `strokeDefault` |
> | `option…` (the same eight) | as the question's |
> | `dragIconTint` | `iconTintSecondary` |
> | `backIconTint` | Cancel's title — `textSecondary` |
> | `separatorColor` | `strokeLight` under the navigation bar; between the options the system separator (`pollOptionDivider`) |
> | `submitButtonTextColor`, `submitButtonTextStyle` | Send's title — `textHighlight`, `heading4Medium` |
> | `submitButtonBackgroundColor`, `…DisabledBackgroundColor`, `…CornerRadius`, `…StrokeWidth`, `…StrokeColor` | Send's background — clear |
> | `progressIndicatorColor` | the "Creating Poll…" spinner — `iconTintSecondary` |
> | `errorTextColor`, `errorTextStyle` | the error banner — `error`, `bodyRegular` |

> [tab] UIKit
>
> `CreatePollStyle` is the style type. Unset colours and fonts follow `theme`
> (`CometChatUIKitTheme.shared` by default). Each new form starts with a copy of the static
> `CometChatCreatePoll.style`; set `form.style` to change one form.
>
> ```swift theme={null}
> let style = CreatePollStyle()
> style.sendButtonTextColor = .systemIndigo
> style.questionInputBoxBorderWidth = 1
> style.questionInputBoxBorderColor = .separator
> style.dragButtonTintColor = .systemIndigo
>
> let form = CometChatCreatePoll()
> form.style = style
> ```
>
> | Property | Default |
> | - | - |
> | `backgroundColor` | `background1` |
> | `borderColor`, `borderWidth`, `cornerRadius` | `strokeLight`, 0, 12 |
> | `titleColor`, `titleFont` | `textPrimary`, the system headline |
> | `cancelButtonTextColor` | `textSecondary` |
> | `questionTitleTextColor`, `questionTitleTextFont` / `optionsTitleTextColor`, `optionsTitleTextFont` | the section headers: `textPrimary`, `heading4Regular` |
> | `questionTextColor`, `questionTextFont`, `questionPlaceholderColor` | `textPrimary`, `bodyMedium`, `textTertiary` |
> | `questionInputBoxCornerRadius`, `questionInputBoxBorderWidth`, `questionInputBoxBorderColor` | 8, 0, `strokeDefault` |
> | `optionsTextColor`, `optionsTextFont`, `optionsPlaceholderColor` | `textPrimary`, `bodyMedium`, `textTertiary` |
> | `optionsInputBoxBackground` | `background3` |
> | `optionsInputBoxCornerRadius`, `optionsInputBoxBorderWidth`, `optionsInputBoxBorderColor` | 8, 0, `strokeDefault` |
> | `dragButtonImage`, `dragButtonTintColor` | the drag handle, `iconTintSecondary` |
> | `separatorColor` | the options' dividers — the system separator (`pollOptionDivider`) |
> | `sendButtonTextColor`, `sendButtonTextFont` | `textHighlight`, `heading4Medium`; disabled, `neutral400` |
> | `sendButtonBackgroundColor`, `disabledSubmitButtonBackgroundColor` | clear |
> | `sendButtonCornerRadius`, `sendButtonBorderWidth`, `sendButtonBorderColor` | 8, 0, clear |
> | `progressIndicatorColor` | `iconTintSecondary` |
> | `errorTextColor`, `errorTextFont` | `error`, `bodyRegular` |
>
> The form dismisses with the text button Cancel, so there is no back icon to style. The
> question box is `background3` and has no style property of its own.

***

## Next Steps

> [card] [Message Composer](https://www.cometchat.com/docs/ui-kit/ios/v6/message-composer.md)
>
> Hosts this form behind the Poll option

> [card] [Sticker Keyboard](https://www.cometchat.com/docs/ui-kit/ios/v6/sticker-keyboard.md)
>
> The composer's other hosted component


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.