UIAlertView延迟或不显示

时间:2015-06-02 21:01:58

标签: ios objective-c uialertview

我已多次使用UIAlertView而没有任何问题,但这次我无法使其正常工作。 (简单)代码如下:

long

如果我触摸TableView的一行,我有两种不同的行为:

  1. 在一些(6/7)秒后显示alertView
  2. 未显示alertView。在这种情况下,只要我触摸任何一个 屏幕上的警报立即显示。
  3. 在这两种情况下,[alert show]在第一次触摸后立即执行,因为我在日志屏幕中看到“已执行”。 在警报之前和之后,应用程序没有做任何其他事情。

    任何帮助?

3 个答案:

答案 0 :(得分:2)

试试这个

 # Below I am importing the modules that I will need
import pygame
import random
import time
from pygame.locals import *

# This initiates pygame
pygame.init()

# These are my defined colours
white = (255,255,255)
lightGrey = (200,200,200)
black = (0,0,0)
grey = (100,100,100)
red = (255,0,0)
yellow = (200,200,0)
green = (34,177,76)

# This sets the display width and height
display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Major League Mathematics')

clock = pygame.time.Clock()

FPS = 30


easyvalues = list(range(10, 100 + 1))
mediumvalues = list(range(100, 1000 + 1))
hardvalues = list(range(1000, 10000 + 1))

# These are my set sizes for my message_to_screen definition
smallfont = pygame.font.SysFont("Arial", 30)
medfont = pygame.font.SysFont("Arial", 50)
largefont = pygame.font.SysFont("Arial", 100)
menufont = pygame.font.SysFont("Arial", 80)

# The next 3 definitions define how text is displayed
def text_objects(text,color,size):

# This is an 'if' statement
    if size == "small":
        textSurface = smallfont.render(text, True, color)
    elif size == "medium":
        textSurface = medfont.render(text, True, color)
    elif size == "large":
        textSurface = largefont.render(text, True, color)
    elif size == "menu":
        textSurface = menufont.render(text, True, color)

    return textSurface, textSurface.get_rect()

def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "medium"):
    textSurf, textRect = text_objects(msg,color,size)
    textRect.center = ((buttonx + (buttonwidth / 2)), buttony + (buttonheight / 2))
    gameDisplay.blit(textSurf, textRect)

def message_to_screen(msg,color,y_displace = 0,size = "small"):
    textSurf, textRect = text_objects(msg,color,size)
    textRect.center = (display_width / 2), (display_height / 2) + y_displace
    gameDisplay.blit(textSurf, textRect)


def button(text, color, x, y, width, height, inactive_color, active_color, value = None):
    cur = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + width > cur[0] > x and y + height > cur[1] > y:
        pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
        if click[0] == 1 and value != None:
            if value == "easy":
                easyvalues = list(range(10, 100 + 1))
                sum1 = random.choice(easyvalues)
                sum2 = random.choice(easyvalues)
                gameLoop()
            if value == "medium":
                mediumvalues = list(range(100, 1000 + 1))
                sum1 = random.choice(mediumvalues)
                sum2 = random.choice(mediumvalues)
                gameLoop()
            if value == "hard":
                hardvalues = list(range(1000, 10000 + 1))
                sum1 = random.choice(hardvalues)
                sum2 = random.choice(hardvalues)
                gameLoop()
            else:
                easyvalues = list(range(10, 100 + 1))
                sum1 = random.choice(easyvalues)
                sum2 = random.choice(easyvalues)
                gameLoop()

    else:
        pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))

    text_to_button(text, black, x, y, width, height)



def question():
    if value == "easy":
        sum1 = random.choice(easyvalues)
        sum2 = random.choice(easyvalues)
    if value == "medium":
        sum1 = random.choice(mediumvalues)
        sum2 = random.choice(mediumvalues)
    if value == "hard":
        sum1 = random.choice(hardvalues)
        sum2 = random.choice(hardvalues)
    else:
        sum1 = random.choice(easyvalues)
        sum2 = random.choice(easyvalues)
    print sum1
    print sum2
    print sum1 + sum2
    print value


#Here I am trying to enable raw input from the user
def answer():

    answer = ""
    font = pygame.font.SysFont("Arial", 50)
    pygame.draw.rect(gameDisplay, white, [200,250,400,100])


    message_to_screen("What is: " + str(sum1) + " + " + str(sum2),
                      black,
                      -100,
                      "medium")
    input = True
    while input:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.unicode.isdigit():
                    answer += event.unicode
                if event.key == K_BACKSPACE:
                    answer = answer[:-1]


# This is temporary, while I program the rest
                elif event.key == K_RETURN:
                    if answer == sum1 + sum2:
                        message_to_screen("Correct!", green, 0, "large")
                    else:
                        message_to_screen("Wrong!", red, 0, "large")
                elif event.key == pygame.K_KP_ENTER:
                    if answer == sum1 + sum2:
                        message_to_screen("Correct!", green, 0, "large")
                    else:
                        message_to_screen("Wrong!", red, 0, "large")



                elif event.key == K_ESCAPE:
                    pygame.quit()
                    quit()
            elif event.type == pygame.QUIT:
                pygame.quit()
                quit()


        block = font.render("Answer: " + answer, True, black)
        rect = 210,280
        gameDisplay.blit(block, rect)
        pygame.display.flip()

















## This is a reference so that I can get my code working

##def name():
##    pygame.init()
##    screen = pygame.display.set_mode((480, 360))
##    name = ""
##    font = pygame.font.Font(None, 50)
##    while True:
##        for evt in pygame.event.get():
##            if evt.type == KEYDOWN:
##                if evt.unicode.isalpha():
##                    name += evt.unicode
##                elif evt.key == K_BACKSPACE:
##                    name = name[:-1]
##                elif evt.key == K_RETURN:
##                    name = ""
##            elif evt.type == QUIT:
##                return
##        screen.fill((0, 0, 0))
##        block = font.render(name, True, (255, 255, 255))
##        rect = block.get_rect()
##        rect.center = screen.get_rect().center
##        screen.blit(block, rect)
##        pygame.display.flip()












##def input_box():
##
##    pygame.draw.rect(gameDisplay, grey, [(display_width / 2) - 150,(display_height / 2) + 160,300,80])
##    
##
##    for event in pygame.get():
##        if event.type == pygame.KEYDOWN:



# This definition defines a start screen so that my game doesnt play straight away
def startScreen():

    menu = True

    while menu:

# This allows me to quit the game without any problems
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    quit()

# This takes away the menu screen and starts the game once you've pressed 'Enter'
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                menu = False

        gameDisplay.fill(grey)
        message_to_screen("Major League Mathematics", black, -200, "menu")


        #button building
        button("Easy", black, 100, 220, 150, 100, lightGrey, white, value = "easy")
        button("Medium", black, 325, 220, 150, 100, lightGrey, white, value = "medium")
        button("Hard", black, 550, 220, 150, 100, lightGrey, white, value = "hard")

        message_to_screen("Select a difficulty to start!", black, 250, "small")
        pygame.display.update()



# This is my main loop that my game will run off
def gameLoop():

    gameExit = False
    gameOver = False




    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    quit()




        gameDisplay.fill(grey)
        answer()
        pygame.display.update()

        clock.tick(FPS)

    pygame.quit()
    quit()


startScreen()

答案 1 :(得分:0)

信不信由你,但实际上在iOS8中已经弃用UIAlertView

话虽如此,你仍然可以使用它。对于未来的项目定位iOS8 + Apple建议您改为使用UIAlertViewController

这是使用UIAlertViewController打开应用程序设置的示例:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Go to settings" message:@"Do you wish to go to the application settings?" preferredStyle:UIAlertControllerStyleAlert]; ;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:@"Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
[alertController addAction:cancelAction];
[alertController addAction:settingsAction];

[self presentViewController:alertController animated:YES completion:nil];

答案 2 :(得分:0)

我也遇到了这个问题,对我来说,问题是我有两个同时运行的队列。我在主队列中发生了UI更改(所有UI更改都必须在主队列中发生),并且我在单独的队列中发生了计算量很大的任务。我最初的设置是:

let alert = UIAlertController(title: nil, message: "Custom message", preferredStyle: .alert)
self.present(alert, animated: false, completion: nil)

// perform time intensive task in seperate queue

为解决此问题,我将密集任务移至完成回调中:

let alert = UIAlertController(title: nil, message: "Custom message", preferredStyle: .alert)
self.present(alert, animated: false, completion: {
    // perform intensive task here
})