UISegmentedControl重新加载某些页面

时间:2016-11-14 14:42:38

标签: ios swift uisegmentedcontrol

我使用第三方库为我的UISegmentedControl。页面初始化如下:

from rest_framework import viewsets

from customers.models import Customer
from customers.serializers import CustomerSerializer
from api.permissions import IsOwnerOrAdmin

from rest_framework import authentication, permissions, status
from rest_framework.response import Response


class CustomerViewSet(viewsets.ModelViewSet):
    serializer_class = CustomerSerializer
    queryset = Customer.objects.all()
    authentication_classes = (authentication.TokenAuthentication,
                              authentication.SessionAuthentication,
                              authentication.SessionAuthentication, )

    def get_permissions(self):
        if self.action == 'list':
            self.permission_classes = (permissions.IsAdminUser,)
        elif self.action == 'create':
            self.permission_classes = (permissions.AllowAny,)

        return super(self.__class__, self).get_permissions()

    def create(self, request, *args, **kwargs):
        print "This is view create -----------------------------"
        serializer = self.get_serializer(data=request.data)
        # print serializer

        try:
            serializer.is_valid(raise_exception=True)
            self.perform_create(serializer)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
        except ValidationError as e:
            if e.detail.get('username') == ['user with this username already exists.']:
                raise Custom409()
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

当我按下第三段时,用户可以进一步查看更多详细信息(通过同一页面上的子视图)。每次我再次选择第三段时,我希望页面重新加载。 (回到原来的CategoriesFolderTab页面)。目前我使用func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAtIndex index: UInt) -> UIViewController { switch index { case 0: return self.storyboard!.instantiateViewControllerWithIdentifier("FolderOverviewController") as! FolderOverviewController case 1: return self.storyboard!.instantiateViewControllerWithIdentifier("TopFoldersTab") as! TopFoldersTab case 2: return self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab default: return self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab } } 执行此操作,但是当您多次执行此操作时,这会降低应用程序的速度。

有更正确的方法吗?提前致谢

2 个答案:

答案 0 :(得分:0)

我认为调用viewDidLoad()不是实现此目的的正确方法,而是实现一个新函数,该函数应包含负责在UI组件中加载数据的代码,例如:

override func viewDidLoad() {
    super.viewDidLoad()

    reloadUI()
}

func reloadUI() {
    // filling UI components with desired data, such as:
    // myLabel.text = "Hello World"
}

在您的代码中的某个位置(您要重新加载的位置),而不是调用viewDidLoad(),您应该调用reloadUI()方法。

希望这会有所帮助。

答案 1 :(得分:0)

试试这个

RETURN JSON_MERGE('{"id":1,"name":"Molly","Age":24}', '{"id":2,"name":"Edward","Age":28}');

这会初始化视图控制器一次,因此一旦初始化它们就会调用它们各自的viewDidLoad方法,而不是每次点击一个标签项