检查设备是否为iPad

时间:2012-10-17 23:17:03

标签: iphone ios ipad user-interface ios6

这段代码有什么问题?我希望能够检查用户当前使用的设备是否是iPad,但它一直给我错误。

if (UIUserInterfaceIdiom == UIUserInterfaceIdiomPad)
{
    //do stuff
}

5 个答案:

答案 0 :(得分:44)

您可以使用

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

答案 1 :(得分:12)

在Swift中你可以使用:

if UIDevice.current.userInterfaceIdiom == .pad {
    //do stuff
}

答案 2 :(得分:8)

你必须检查设备是否是iphone / ipad的条件

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
//do ur  ipad logic
}else
{
//do ur  iphone logic
}

答案 3 :(得分:3)

如果您使用的是swift,请使用此

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad)
    {
      // device is ipad
    }
    else
    {
        //  device is iPhone
    }

答案 4 :(得分:1)

  

/ *提供UI_USER_INTERFACE_IDIOM()函数,以便在部署到小于3.2的iOS版本时使用。如果您要部署的最早版本的iPhone / iOS是3.2或更高版本,您可以直接使用 - [UIDevice userInterfaceIdiom]。    * /

除非您支持超级旧iOS,否则最好使用 /** * @Route("/intake/addItem/{id}", name="intake_addItem") */ public function addItemAction($id, Request $request) { $donor=$this->getDoctrine() ->getRepository('AppBundle:Donor') ->find($id); $types=$this->getDoctrine() ->getRepository('AppBundle:Types') ->findAll(); $categories=$this->getDoctrine() ->getRepository('AppBundle:Category') ->findAll(); $CW=$this->getDoctrine() ->getRepository('AppBundle:Cardwheel') ->findBy(array('idyear'=>'1')); $raffle=$this->getDoctrine() ->getRepository('AppBundle:Raffle') ->findBy(array('idyear'=>'1')); $Item = new Item; $form =$this->createFormBuilder($Item) ->add('Description', Type\TextType::class, ['attr' => ['class'=> 'form-control']] ) ->add('Startingbid', Type\TextType::class, ['attr' => ['class'=> 'form-control']] ) ->add('BidIncrement', Type\TextType::class, ['attr' => ['class'=> 'form-control']] ) ->add('ItemValue', Type\TextType::class, ['attr' => ['class'=> 'form-control']] ) ->add('ItemNumber', Type\TextType::class, ['attr' => ['class'=> 'form-control']] ) ->add('idcategory', EntityType::class, ['class' => Category::class, 'attr' => ['class'=> 'form-control']] ) ->add('Save', Type\SubmitType::class, ['attr' => ['class'=> 'btn btn-success']] ) ->getForm(); $form->handleRequest($request); if($form->isSubmitted() && $form->isValid()) { $this->addFlash('notice', 'Item Added'); return $this->redirectToRoute('intake_listItems'); } return $this->render('intake/addForm.html.twig',array('form' => $form->createView(),'Article'=>'Item')); }

可能的情况如下:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Item
 *
 * @ORM\Table(name="Item", indexes={@ORM\Index(name="fkYear_idx", columns={"idYear"}), @ORM\Index(name="fkItemCategory_idx", columns={"idCategory"}), @ORM\Index(name="fkItemType_idx", columns={"idType"}), @ORM\Index(name="fkItemCardWheel_idx", columns={"idCardWheel"}), @ORM\Index(name="fkItemRaffle_idx", columns={"idRaffle"})})
 * @ORM\Entity
 */
class Item
{
/**
 * @var integer
 *
 * @ORM\Column(name="idRaffle", type="integer", nullable=true)
 */
private $idraffle;

/**
 * @var string
 *
 * @ORM\Column(name="Description", type="string", length=45, nullable=true)
 */
private $description;

/**
 * @var string
 *
 * @ORM\Column(name="StartingBid", type="decimal", precision=7, scale=2, nullable=true)
 */
private $startingbid;

/**
 * @var string
 *
 * @ORM\Column(name="BidIncrement", type="decimal", precision=7, scale=2, nullable=true)
 */
private $bidincrement;

/**
 * @var string
 *
 * @ORM\Column(name="ItemValue", type="decimal", precision=7, scale=2, nullable=true)
 */
private $itemvalue;

/**
 * @var integer
 *
 * @ORM\Column(name="ItemNumber", type="integer", nullable=true)
 */
private $itemnumber;

/**
 * @var integer
 *
 * @ORM\Column(name="idItem", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $iditem;

/**
 * @var \AppBundle\Entity\Cardwheel
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Cardwheel")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="idCardWheel", referencedColumnName="idCardWheel")
 * })
 */
private $idcardwheel;

/**
 * @var \AppBundle\Entity\Category
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="idCategory", referencedColumnName="idCategory")
 * })
 */
private $idcategory;

/**
 * @var \AppBundle\Entity\Types
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Types")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="idType", referencedColumnName="idType")
 * })
 */
private $idtype;

/**
 * @var \AppBundle\Entity\Event
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Event")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="idYear", referencedColumnName="idYear")
 * })
 */
private $idyear;

public function getIdraffle() {
  return $this->idraffle;
}

public function getDescription() {
  return $this->description;
}

public function getStartingbid() {
  return $this->startingbid;
}

public function getBidincrement() {
  return $this->bidincrement;
}

public function getItemvalue() {
  return $this->itemvalue;
}

public function getItemnumber() {
  return $this->itemnumber;
}

public function getIditem() {
  return $this->iditem;
}

public function getIdcardwheel(): \AppBundle\Entity\Cardwheel {
  return $this->idcardwheel;
}

public function getIdcategory(): \AppBundle\Entity\Category {
  return $this->idcategory;
}

public function getIdtype(): \AppBundle\Entity\Types {
  return $this->idtype;
}

public function getIdyear(): \AppBundle\Entity\Event {
  return $this->idyear;
}

public function setIdraffle($idraffle) {
  $this->idraffle = $idraffle;
}

public function setDescription($description) {
  $this->description = $description;
}

public function setStartingbid($startingbid) {
  $this->startingbid = $startingbid;
}

public function setBidincrement($bidincrement) {
  $this->bidincrement = $bidincrement;
}

public function setItemvalue($itemvalue) {
  $this->itemvalue = $itemvalue;
}

public function setItemnumber($itemnumber) {
  $this->itemnumber = $itemnumber;
}

public function setIditem($iditem) {
  $this->iditem = $iditem;
}

public function setIdcardwheel(\AppBundle\Entity\Cardwheel $idcardwheel) {
  $this->idcardwheel = $idcardwheel;
}

public function setIdcategory(\AppBundle\Entity\Category $idcategory) {
  $this->idcategory = $idcategory;
}

public function setIdtype(\AppBundle\Entity\Types $idtype) {
  $this->idtype = $idtype;
}

public function setIdyear(\AppBundle\Entity\Event $idyear) {
  $this->idyear = $idyear;
}


}