菜鸟教程小白 发表于 2022-12-11 20:47:27

ios - appdelegate.m 中的 UIAlertView 不起作用


                                            <p><p>我已经在谷歌上搜索了将近一整天的时间,但没有找到任何解决方案,所以我想问你们...... :)</p>

<p>我正在开发一个 iOS 应用程序,该应用程序应通过 WiFi 连接到 mbed,并在连接时为用户提供一个对话框,如果未连接,则为用户提供重试的可能性。
我的问题是现在我已经在 appdelegate.m 中实现了连接方法,我想从这里显示警报.. </p>

<p>它自己的警报工作正常,但我在检测按下按钮时遇到问题,没有调用 clickedButtonAtIndex。</p>

<p>我在 appdelegate.h 中添加了 UIAlertViewDelegate,如下所示:</p>

<pre><code>@interface AppDelegate : NSObject &lt;UIApplicationDelegate, UITabBarControllerDelegate, UIAlertViewDelegate&gt;
</code></pre>

<p>并在警报 View 中将委托(delegate)设置为 self,如下所示:</p>

<pre><code>alert_NOT = [ initWithTitle:@&#34;Not connected!&#34; message:message_to_user delegate:self cancelButtonTitle:@&#34;Try again&#34; otherButtonTitles: nil];
    ;
   
</code></pre>

<p>clickedButtonAtIndex 看起来像</p>

<pre><code>- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@&#34;test&#34;);
</code></pre>

<p>}</p>

<p>因此,当在警报 View 中按下按钮时,我希望在日志中看到“测试”一词,但没有任何反应。</p>

<p>更新:
尝试在我的“FirstViewController.m”中实现它并且它在那里工作:S 但如果可能的话,我非常希望将它放在 appdelegate.m 中.. </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我目前正在研究类似的实现,并想与您分享一个想法:也许使用 NSNotification 在您的委托(delegate)条件满足时触发,可以在您的 VC 中监听并处理适本地,在堆栈顶部带有警报 View 。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - appdelegate.m 中的 UIAlertView 不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8601274/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8601274/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - appdelegate.m 中的 UIAlertView 不起作用